In an Apigility driven Zend Framework 2 application wenn a database adapter is created (over the Apigility Admin UI), its settings by default get saved in /config/autoload/global.php
return array(
'db' => array(
'adapters' => array(
...
'DbAdapter_FooBar' => array(),
...
),
),
...
);
and in /config/autoload/local.php
return array(
'db' => array(
'adapters' => array(
...
'DbAdapter_FooBar' => array(
'charset' => 'UTF-8',
'database' => 'asdf',
'driver' => 'PDO_Mysql',
'hostname' => 'asdf',
'username' => 'asdf',
'password' => 'asdf',
'port' => '1234',
'driver_options' => array(
1002 => 'SET NAMES \'UTF8\'',
),
),
...
),
),
...
);
In the application I'm working on the config files structure differs from the ZF2 standard, e.g. there are separate config files for the database settings: /config/autoload/mydb.global.php and /config/autoload/mydb.local.php.
(How) Can Apigility be configured in the way, that the database adapters settings get stored in custom config files? How/where to set these files?
Configuration for various components can be provided in multiple places, but all configuration is merged recursively into one large configuration file by the Zend\Stdlib\ArrayUtils::merge() method. Because configurations are merged recursively, the order that they are added to the merged configuration array is really important to avoid unexpected overwriting.
Configurations are merged in the following order:
The array returned by the module’s getConfig() method
config/autoload/*.global.php — global autoload files
config/autoload/*.local.php — local autoload files
The methods described by the Feature interface — get*Config()
Related
I'm going through the zend 2 getting started tutorial and I hit a wall. I am at the point in the tutorial where my action controller loads a view via the indexAction():
public function indexAction() {
return new ViewModel(array(
//$albums inside index.phtml will contain data from this method
'albums' => $this->getAlbumTable()->fetchAll()
));
}
But when loading the page I see this error:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file
At this point I realized I don't know what the hell is happening. I don't even know where to begin troubleshooting this error. Before I scan all of the files for typos I'd really like to understand how this error can occur.
here is my modul.config.php:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' =>
'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'ablum' => __DIR__ . '/../view',
),
),
);
The error "Unable to render template "album/album/index" means that you have to add the index.phtml file under the /album/album directory under the 'Album' module's view directory. The index.phtml view template file is used for rendering the view for the index action of the AlbumController controller of the Album module. Because this file seems to be missing, the view template resolver couldn't find it.
In Zend Framework 2, you implement a view as a template file, which is a file
having .phtml extension ("phtml" stands for PHP+HTML). View templates have such
a name because they usually contain HTML code mixed with PHP code snippets used
for rendering the web pages. Views typically live inside of the view subdirectory of the module.
For beginner, I would recommend to read the Using Zend Framework 2 book. With this e-Book, you can save your time and efforts learning ZF2.
Also, the path is case sensitive i.e. it should be all be in lowercase album/index/index both folders' name and index.phtml else phpRenderer will not be able to trace the view file and render it.
You can also check if you have the following in your module.config.php
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],
I want add in my ZF2 application at begining of every mysql connection this mysql query/setting
SET time_zone = \'+00:00\';
for some reasons i couldn't do it on mysql conifiguration file.
So where i can do it?
(now i create dbadapter by config file:
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
etc...
)
Try this in your config file (for example in config/autoload/global.php)
return array(
'db' => array(
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET time_zone = \'+00:00\''
)
)
I'm using Zend Framework 2 and I've installed the module ZfcUser.
Everything was working correctly until I've decided to add a second db adapter in my config/autoload/global.php
<?php
return array(
'db' => array(
'adapters' => array(
'cleardb' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=secret;host=secret.com',
'driver_options' => array(
...
),
),
'other' => array(...)
)
),
'service_manager' => array(
'abstract_factories' => array(
'Zend\Db\Adapter\AdapterAbstract'
=> 'Zend\Db\Adapter\AdapterAbstractServiceFactory',
),
),
);
Now, ZfcUser can't find my default db adapter.
In the zfcuser.global.php I've updated this line:
'zend_db_adapter' => 'cleardb'
This is the error I get when I try to login:
An alias "zfcuser_zend_db_adapter" was requested but no service could be found.
Your help is really appreciated.
I am trying to replace Figlet with reCaptcha on a zfcUser registration form. Partial instruction on how to accomplish this can be found on https://github.com/ZF-Commons/ZfcUser#changing-registration-captcha-element but no complete instruction exists.
Checking the README.md file has a two-step instruction on how to accomplish this but still the CAPTCHA uses Figlet when rendered on the form.
Has anyone successfully implemented this? I really need a hand on this one.
Thanks in advance.
EDIT: Here is a proven working solution I developed:
1. Add to composer.json
// Add the lines below under the "require" element:
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": ">2.2.0rc1",
"zendframework/zendservice-recaptcha": "2.*"
}
2. Goto to your project's ZF2 installation directory and execute this command:
php composer.phar update
3. Replace or Create config/autoload/database.global.php with:
<?php
$config = array(
'dbdriver' => 'pdo',
'dbhost' => 'localhost',
'dbport' => '3306',
'dbname' => 'CHANGEME',
'dbuser' => 'CHANGEME',
'dbpass' => 'CHANGEME',
);
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname='.$config['dbname'].';host='.$config['dbhost'],
'username' => $config['dbuser'],
'password' => $config['dbpass'],
),
);
4: Execute this on your mySQL server:
CREATE TABLE `user`
(
`user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(255) DEFAULT NULL UNIQUE,
`email` VARCHAR(255) DEFAULT NULL UNIQUE,
`display_name` VARCHAR(50) DEFAULT NULL,
`password` VARCHAR(128) NOT NULL,
`state` SMALLINT UNSIGNED
) ENGINE=InnoDB CHARSET="utf8";
5. Create/Replace config/autoload/recaptcha.global.php with:
<?php
define('RECAPTCHA_PRIVATE_KEY','CHANGEME');
define('RECAPTCHA_PUBLIC_KEY','CHANGEME');
return array(
'zfcuser' => array(
'form_captcha_options' => array(
'class' => 'Zend\Captcha\ReCaptcha',
'options' => array(
'privkey' => RECAPTCHA_PRIVATE_KEY,
'pubkey' => RECAPTCHA_PUBLIC_KEY,
),
),
),
'di'=> array(
'instance'=>array(
'alias'=>array(
'recaptcha_element' => 'Zend\Form\Element\Captcha',
),
'ZfcUser\Form\Register' => array(
'parameters' => array(
'captcha_element'=>'recaptcha_element',
),
),
),
),
);
6. Create/Replace config/autoload/zfcuser.global.php with:
<?php
$settings = array(
'enable_registration' => true,
'enable_username' => true,
'auth_adapters' => array( 100 => 'ZfcUser\Authentication\Adapter\Db' ),
'enable_display_name' => false,
'auth_identity_fields' => array( 'email' ),
'use_registration_form_captcha' => true,
'user_login_widget_view_template' => 'zfc-user/user/login.phtml',
);
return array(
'zfcuser' => $settings,
'service_manager' => array(
'aliases' => array(
'zfcuser_zend_db_adapter' => (isset($settings['zend_db_adapter'])) ? $settings['zend_db_adapter']: 'Zend\Db\Adapter\Adapter',
),
),
);
7. Navigate to http://yourdomain.com/user
8. Enjoy! :)
This is how I did it, it might not be the best or correct way but it worked for me:
Add the recaptcha service to your composer.json file:
"require": {
"Zendframework/zendservice-recaptcha": "2.*"
}
Run composer to get the service. Then you need to specify the ReCaptcha config.
I created a separate config file to store the ReCaptcha keys:
//zfcuser.local.php
return array(
'zfcuser' => array(
'form_captcha_options' => array(
'options' => array(
'privkey' => RECAPTCHA_PRIVATE_KEY,
'pubkey' => RECAPTCHA_PUBLIC_KEY,
),
),
),
);
Then ZfcUser captcha config looks like so, telling it to use the ReCaptcha service:
//zfcuser.global.php
'form_captcha_options' => array(
'class' => 'Zend\Captcha\ReCaptcha',
'options' => array(
'wordLen' => 6,
'expiration' => 300,
'timeout' => 300,
),
),
Edit:
You don't need the recaptcha.global.php. You can call the config file whatever you like aslong as it ends with .global.php or .local.php. You usually name things .local.php when you don't want them in version control.
In this case I named the file zfcuser.local.php because all it does is store the ReCaptcha keys and I didn't want them in version control.
All the config files get merged in to one array when the application is started. So basically, ignore the ZfcUser documentation. Or maybe someone else can explain how to get it working that way.
The third block of code is the zfcuser.global.php.
I'm creating a simple restful api using zend framework2 and I've references Rob Allen's notes on the subject as well as this excellent tutorial by http://hounddog.github.com/blog/getting-started-with-rest-and-zend-framework-2/
below is my module_config.php. You'll see that I have routes and the JSON view strategy configured. My understanding is that when you set up the JSON strategy in this fashion it accommodates all modules. The issue is that when an invalid route is entered the 404 response is sent back in html even though the Accept header is requesting Application/json.
I've been struggling with this for a 2 days now any advice or help would be appreciated
This curl call to the api generates the expected 404 error.
curl -i -H "Accept: application/json" http://myapi-dev.local/xxx/1
Module_config.php
return array(
'controllers' => array(
'invokables' => array(
'Dips\Controller\Roles' => 'Dips\Controller\RolesController', //maps controller alias to a physical controller
),
),
'router' => array(
'routes' => array(
'dips' => array(
'type' => 'segment',
'options' => array(
'route' => '/dips/roles/:apikey/:uname/:appname',
'constraints' => array(
'apikey' => '[a-zA-Z0-9]+',
'uname' => '[a-zA-Z]+',
'appname' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => 'Dips/Controller/Roles',
),
),
),
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
);
You would need to implement an additional rendering strategy and put it in the stack above the default 404 rendering strategy. Alternatively, you could extend the existing ViewJsonStrategy to include error handling.
The default 404 Strategy is Zend/Mvc/View/Http/RouteNotFoundStrategy. If you look at it's detectNotFoundError method, you can see when it gets triggered.