Leaflet extension and Geosearch Directory not found - yii2

Im relatively new to the Yii framework. I'm trying to get leaflet extension to work on my project. I installed it via composer and everything seems to be installed properly but when I try to insert it into an activeForm it throws me this error:
The file or directory to be published does not exist: /vendor/bower/leaflet/dist
Im trying to build a Geosearch field with a map.
The extensions I have installed are:
"2amigos/yii2-leaflet-extension": "~1.0",
"2amigos/yii2-leaflet-geocoder-plugin": "~1.0",
"2amigos/yii2-leaflet-geosearch-plugin": "~1.0"
This is my code on the ActiveForm (its just the same as the provided on GitHub):
use dosamigos\leaflet\layers\TileLayer;
use dosamigos\leaflet\LeafLet;
use dosamigos\leaflet\types\LatLng;
use dosamigos\leaflet\plugins\geosearch\GeoSearch;
use dosamigos\leaflet\widgets\Map;
<?php
$center = new LatLng(['lat' => 39.67442740076734, 'lng' => 2.9347229003906246]);
$geoSearchPlugin = new GeoSearch([
'service' => GeoSearch::SERVICE_OPENSTREETMAP
]);
$tileLayer = new TileLayer([
'urlTemplate' => 'link not allowed because its one of my first posts...',
'clientOptions' => [
'attribution' => 'Tiles Courtesy of MapQuest ' . ', ' . 'Map data © OpenStreetMap contributors, ' . 'CC-BY-SA', 'subdomains' => '1234'
]
]);
$leafLet = new LeafLet([
'name' => 'geoMap',
'tileLayer' => $tileLayer,
'center' => $center,
'zoom' => 10,
'clientEvents' => [ // setting up one of the geo search events for fun
'geosearch_showlocation' => 'function(e){ console.log(e.target); }'
]
]);
// add the plugin
$leafLet->installPlugin($geoSearchPlugin);
// run the widget (you can also use Map::widget([...]))
echo $leafLet->widget();
?>
Is there something I'm missing or doing wrong?
Thanks in advanced

Your problem is that the original Leaflet js and css files are no longer available through Bower, and 2amigos have not changed their composer file to reflect the changes. Yii is attempting to publish a css file from the requested location, and the file doesn't yet exist, hence the error message. You'll either need to go to the original Leaflet repository and copy the relevant files into the required directories, or update the composer file to point to the original files on GitHub. Sorry, I don't know enough about composer to be able to help with this bit!

Related

Yii2: Translate Application Name

How to properly translate the name of the application in Yii2?
We can easily set the application name in main-local.php (or config/main.php) like so:
$config = [
'name' => 'My Application Name',
// ...
];
But how would we translate it?
Using something like \Yii::t('app.name', 'My Application Name') does not work because the configuration file is parsed before the application language is even determined or set.
The easiest way is to do translation on actual usage of application name:
<?= \Yii::t('app.name', Yii::$app->name) ?>
For messages extraction you may use fake translation in comment. Not sure about Poedit, but built-in Yii extractor support this some time ago:
$config = [
// \Yii::t('app.name', 'My Application Name')
'name' => 'My Application Name',
// ...
];
In worst case you may create separate file for such fake translations only for messages extraction, and don't include it on actual execution.

How to change default template in Yii2?

I am using the Yii 2 Advanced Application Template, the AdminLTE Asset Bundle and the Gii code generator.
Here is my example:
I need to change the template so I can remove the "Create Lab Tipos Movimientos" button (and modify some things more).
I am removing every button after Gii create the CRUD but I would like to change the template so Gii can do it automatically.
Once you have create your own template for gii
You can change the default template for gii assigning the new template values in main.php (main-local.php)
assigning the proper parameter to gii module
.....
$config['modules']['gii'] = [
//'class' => 'yii\gii\Module',
'class' => 'your_gii_module_path\Module',
'allowedIPs' => ['127.0.0.1', '::1', ],
'generators' => [ //here
'crud' => [ // generator name
//'class' => 'yii\gii\generators\crud\Generator', // generator class
'class' => 'your_gii_module_path\generators\crud\Generator', // generator class
'templates' => [ //setting for out templates
// template name => path to template
'your_crud_entry' => 'your_absolute_path_\your_gii_module_path\generators\crud\default',
]
]
],
];
.......
I have not done it myself, but I found this Guide by SamDark in Github that explains how to create your own template. This is the url: https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/topics-creating-your-own-templates.md
Additionally, if you just want to eliminate the "Create Lab Tipos Movimiento" button you can try modifying the current template which if I am not wrong is located inside the folder vendor/yiisoft/yii2-gii/generators/crud/default/views and the file should be index.php. There you can try deleting or better yet commenting the part of the code that says:
<p>
<?= "<?= " ?>Html::a(<?= $generator->generateString('Create ' . Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>, ['create'], ['class' => 'btn btn-success']) ?>
</p>
I suggest you to make a copy of the files you modify just in case anything goes wrong.
Hope this helps you.
Have a great day.
EDIT:
Additionally following the answer of schmunk to a very similar question in stack overflow found here: How to use custom templates in gii (using Yii 2)
There is apparently a Gii extension in beta phase to help you in this situation called yii2-giiant found here: https://github.com/schmunk42/yii2-giiant (maybe there are similar extensions that are in a more advanced phase of development, google search should help with that)

How to use telegram api package in CakePHP 3?

I tried to use PHP telegram bot in my CakePHP 3 website, but encountered many problems.
done
I ran "composer require longman/telegram-bot" and copied package to "plugins" folder.
and then "bin/cake plugin load longman/telegram-bot".
questions
1. Where I must put the package and why? vendor or plugins
2. How to call package methods in URL?
I added "index" method to "telegram-bot/src/Telegram.php" and tried this snippet code in my "Template/Users/index.ctp".
echo $this->Html->link('telegram', ['plugin' => 'longman/telegram-bot', 'controller' => 'telegram', 'action' => 'index']);
that results
Error: A route matching "array ( 'plugin' => 'longman/telegram-bot', 'controller' => 'telegram', 'action' => 'index', '_ext' => NULL, )" could not be found.
3. What I must add to "routes.php"?
4. How can I use "telegram-bot/src/Telegram.php" methods in my controllers and models.
5. Is there anything else that must be observed?
Thanks.

Cakephp 3.0 download file

I asked a question before about file uploading. I managed to get it working and my next step is getting the file to be downloaded by a customer the files may be different formats from videos to PDFs.
I did read about media views, but they where cakephp 2.0. I have found that 2.0 seems to cause problems for me at least.
I can upload my files for uploading if it helps.
Nased on the link Alex posted, how would I call this function in my view this is the function based from the cakephp book link
public function downloadFile($id)
{
$file = $this->Attachments->getFile($id);
// a view.
$this->response->file(
$file['files/content'],
['download' => true]
);
return $this->response;
}
To get a link to that function (using the cakephp default routing) you would do this in your view:
echo $this->Html->link('Download File', ['controller' => 'Attachments', 'action' => 'download_file', $id]);
public function download(){
$file_path = WWW_ROOT.'uploads'.DS.'file_name.doc';
$this->response->file($file_path, array(
'download' => true,
'name' => 'file_name.ppt',
));
return $this->response;
}

An easy way to load ACL in Zend Framework 2?

I have been following this guide to load my menu configuration and i think it is very nice and clean way to load the menu.
My question is simple, is there a way to load your ACL configuration on the same way with a config array and some kinda of factory?
If there isn't, how do i load a ACL configuration and use with that menu in a easy way?
Thanks!
Edit:
This is a very good blog post on why use modules that is already done and not make your own, http://hounddog.github.com/blog/there-is-a-module-for-that/
ZF2 contains ACL and also RBAC (role based ACL - might be in ZF2.1), but to put it in place, easier is to use module which you can plug into your application. BjyAuthorize seems to me a bit bloated, you have to use ZfcUser module. I prefer ZfcRbac, the ACL rules are based on user roles (group) and their access to controller, action or route. Configuration stored in one config file, really easy to implement.
Most likely there are several ways to do it, but I prefer to do it in getViewHelperConfig() of application's Module.php (here I use BjyAuthorize module to simplify work with ACL, and in particular it allows to set ACL rules in configuration file module.bjyauthorize.global.php)
public function getViewHelperConfig()
{
return array(
'factories' => array(
'navigation' => function($sm) {
$auth = $sm->getServiceLocator()->get('BjyAuthorize\Service\Authorize');
$role = $auth->getIdentityProvider()->getIdentityRoles();
if (is_array($role))
$role = $role[0];
$navigation = $sm->get('Zend\View\Helper\Navigation');
$navigation->setAcl($auth->getAcl())->setRole($role);
return $navigation;
}
)
);
}
Play with This structure . get role and resource from database and save this in session for or any caching .
You are right, there is no out-of-the-box-all-in-one solution. You have to build some bridges between the modules.
Another easy way to integrate BjyAuthorize is using **Zend Navigation**s default methods as described by Rob Allen:
Integrating BjyAuthorize with ZendNavigation
$sm = $e->getApplication()->getServiceManager();
// Add ACL information to the Navigation view helper
$authorize = $sm->get('BjyAuthorizeServiceAuthorize');
$acl = $authorize->getAcl();
$role = $authorize->getIdentity();
ZendViewHelperNavigation::setDefaultAcl($acl);
ZendViewHelperNavigation::setDefaultRole($role);
You can also use ZfcRbac and use a listener to make it work with Zend Navigation.
Since this is a lot of code I simply post the link here:
Check Zend Navigation page permissions with ZfcRbac – Webdevilopers Blog
I've just created an ACL module that creates an ACL Service parsing the routes.
To manage your access control to your application you only need to define roles and add a new key 'roles' in every route. If you do not define that key or its array is empty, then the route becomes public. It also works with child routes.
As an example:
array(
'router' => array(
'routes' => array(
'user\users\view' => array(
'type' => 'Segment',
'options' => array(
'route' => '/admin/users/view/id/:id/',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'User\Controller\Users',
'action' => 'view',
'roles' => ['admin', 'user'],
),
),
),
),
),
);
The module can be installed via composer and it is now listed in the zend modules repository: http://zfmodules.com/itrascastro/TrascastroACL
You can get more detailed info about use and installation from my blog: http://www.ismaeltrascastro.com/acl-module-zend-framework/