Zend autoload not work in bootstrap - zend-framework

i developing a site with zend framework.
i use autoload for load a class.
it work on controller, on model but not work in bootstrap file.
why?
bootstrap.php
protected function _initAutoload ()
{
// Add autoloader empty namespace
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array('basePath' => APPLICATION_PATH, 'namespace' => '',
'resourceTypes' => array(
'form' => array('path' => 'forms/', 'namespace' => 'Form_'),
'model' => array('path' => 'models/', 'namespace' => 'Model_'),
'plugin' => array('path' => 'plugin/', 'namespace' => 'Plugin_'))));
// viene restituto l'oggetto per essere utilizzato e memorizzato nel bootstrap
return $autoLoader;
}
/**
* inizializza l'autenticazione
*/
protected function _initAuth ()
{
$this->bootstrap("db");
$this->bootstrap("Autoload");
$db = $this->getPluginResource('db')->getDbAdapter();
$adp = new Zend_Auth_Adapter_DbTable($db);
$adp->setTableName(USERS_TABLE)
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('sha1(?)');
$storage = new Model_Sessions(false, $db);//line 81
$auth = Zend_Auth::getInstance();
$auth->setStorage($storage);
//$this->bootstrap('log');$log=$this->getResource('log');
if ($auth->hasIdentity()) {
$identity = $auth->getIdentity();
$user = $identity->user_id;
} else
$user = 1;
$user = new Model_user($user);
}
output error
Fatal error: Class 'Model_Sessions' not found in /application/Bootstrap.php on line 81
in Session.php
<?php
/**
* #method get($k,$dv=FALSE)
*/
class Model_Sessions implements Zend_Auth_Storage_Interface
{

Your resource autoloader looks good.
I suspect you want Model_Sessions, not Model_sessions (not lower/upper case on "sessions").
Make sure the class Model_Sessions is stored in file application/models/Sessions.php
As a side note, you have your resource autoloader looking for plugins with namespace prefix plugins_. Again, here, I suspect you want uppercase Plugins_.

Related

Zend Framework 2 - Gobal Variable that are accessible to controller/model that are initialize in local.php or global.php

Hello everyone please someone help me how to create a global variable in zend framework 2 to be use in table prefix that are accessible in controller and model.
Thanks and regards to all.
In your config/database.local.php you can define which you want globally
<?
return array(
'service_manager' => array(
'factories' => array(
//'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
'Zend\Db\Adapter\Adapter' => function ($serviceManager) {
$adapterFactory = new Zend\Db\Adapter\AdapterServiceFactory();
$adapter = $adapterFactory->createService($serviceManager);
\Zend\Db\TableGateway\Feature\GlobalAdapterFeature::setStaticAdapter($adapter);
return $adapter;
}
),
),
'db' => array(
'driver' => 'pdo',
'dsn' => 'mysql:dbname=testdb;host=localhost',
'username' => 'root',
'password' => '',
),
'msg' => array(
'add' => 'Data Inserted Successfully',
'edit' => 'Data Updated Successfully',
'delete' => 'Data Deleted Successfully',
),
);
?>
Controller File:
DemoController.php
<?php
namespace Demo\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class DemoController extends AbstractActionController
{
public function indexAction($cms_page_name='whyus')
{
/*Call config file to fetch current cms page id-- fetch config file from database.local.php*/
$config = $this->getServiceLocator()->get('Config');
$all_msg = $config['msg'];
}
}
?>

How to integrate ZF2 with Doctrine Mongo ODM? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to integrate zf2 beta3 with doctrine mongo odm (https://github.com/doctrine/DoctrineMongoODMModule) but no sucess.
How can I install and configure it?
I'm doing just the same thing. Something like this should work:
Download the module, and place in your vendor folder.
Add the module in application.config.php
Copy module.doctrine_mongodb.config.php.dist to /config/autoload
Edit that config file with your own settings
Change the name of that config file to module.doctrine_mongodb.local.config.php
Create a 'setDocumentManager' method in your controller like this:
protected $documentManager;
public function setDocumentManager(DocumentManager $documentManager)
{
$this->documentManager = $documentManager;
return $this;
}
Place the following in your module's DI config:
'Application\Controller\[YourControllerClass]' => array(
'parameters' => array(
'documentManager' => 'mongo_dm'
)
),
Create Document classes according to the Doctrine 2 documentation, and the clarification in this question and answer: Annotations Namespace not loaded DoctrineMongoODMModule for Zend Framework 2
Finally, use the dm like this:
public function indexAction()
{
$dm = $this->documentManager;
$user = new User();
$user->set('name', 'testname');
$user->set('firstname', 'testfirstname');
$dm->persist($user);
$dm->flush();
return new ViewModel();
}
I will give the steps I have done to integrate zf2 with mongodb doctrine odm
1.Download the mongodb doctrine odm module and place in vendor directory or clone it from github
cd /path/to/project/vendor
git clone --recursive https://github.com/doctrine/DoctrineMongoODMModule.git
2.Copy the file from /path/to/project/vendor/DoctrineMongoODMModule/config/module.doctrine_mongodb.config.php.dist, place in your path/to/your/project/config/autoload/ and rename to module.doctrine_mongodb.local.config.php
3.Edit your module.doctrine_mongodb.local.config.php.
Change the default db
'config' => array(
// set the default database to use (or not)
'default_db' => 'myDbName'
),
Change your connection params
'connection' => array(
//'server' => 'mongodb://<user>:<password>#<server>:<port>',
'server' => 'mongodb://localhost:27017',
'options' => array()
),
Change the driver options
'driver' => array(
'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
'namespace' => 'Application\Document',
'paths' => array('module/Application/src/Application/Document'),
),
Add proxy and hydratros config
'mongo_config' => array(
'parameters' => array(
'opts' => array(
'auto_generate_proxies' => true,
'proxy_dir' => __DIR__ . '/../../module/Application/src/Application/Document/Proxy',
'proxy_namespace' => 'Application\Model\Proxy',
'auto_generate_hydrators' => true,
'hydrator_dir' => __DIR__ . '/../../module/Application/src/Application/Document/Hydrators',
'hydrator_namespace' => 'Application\Document\Hydrators',
'default_db' => $settings['config']['default_db'],
),
'metadataCache' => $settings['cache'],
)
),
4.Create a directory named "Document" in /path/to/project/module/Application/src/Application/ where goes your documents mapping and inside "Document" directory, create "Proxy" and "Hydrators" directories.
5.Edit your /path/to/project/config/application.config.php and add 'DoctrineMongoODMModule' to modules array
6.Be sure you have mongo php extension installed otherwise download at http://www.php.net/manual/en/mongo.installation.php#mongo.installation.windows and copy it to your extension php directory, usually /php/ext. Add the extension line acording the name file extension you have downloaded "extension=php_mongo-x.x.x-5.x-vc9.dll" in your php.ini.
7.Create a document mapping User.php in your document directory application module.
<?php
namespace Application\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/** #ODM\Document */
class User
{
/** #ODM\Id */
private $id;
/** #ODM\Field(type="string") */
private $name;
/**
* #return the $id
*/
public function getId() {
return $this->id;
}
/**
* #return the $name
*/
public function getName() {
return $this->name;
}
/**
* #param field_type $id
*/
public function setId($id) {
$this->id = $id;
}
/**
* #param field_type $name
*/
public function setName($name) {
$this->name = $name;
}
}
8.Persist it, for example in your controller
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\ActionController,
Zend\View\Model\ViewModel,
Application\Document\User;
class IndexController extends ActionController
{
public function indexAction()
{
$dm = $this->getLocator()->get('mongo_dm');
$user = new User();
$user->setName('Bulat S.');
$dm->persist($user);
$dm->flush();
return new ViewModel();
}
}
Now the default config has changed, could you show an updated method to get this working in ZF2?
<?php
return array(
'doctrine' => array(
'connection' => array(
'odm_default' => array(
'server' => 'localhost',
'port' => '27017',
'user' => null,
'password' => null,
'dbname' => 'user',
'options' => array()
),
),
'configuration' => array(
'odm_default' => array(
'metadata_cache' => 'array',
'driver' => 'odm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineMongoODMModule/Proxy',
'proxy_namespace' => 'DoctrineMongoODMModule\Proxy',
'generate_hydrators' => true,
'hydrator_dir' => 'data/DoctrineMongoODMModule/Hydrator',
'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
'default_db' => null,
'filters' => array() // array('filterName' => 'BSON\Filter\Class')
)
),
'driver' => array(
'odm_default' => array(
'drivers' => array()
)
),
'documentmanager' => array(
'odm_default' => array(
'connection' => 'odm_default',
'configuration' => 'odm_default',
'eventmanager' => 'odm_default'
)
),
'eventmanager' => array(
'odm_default' => array(
'subscribers' => array()
)
),
),
);
Currently receiving the error: The class 'Application\Document\User' was not found in the chain configured namespaces

Zend Framework not finding forms in modular application

I have a Zend Framework modular application set up. One of my modules is called 'frontend' and it is the default module (resources.frontController.defaultModule = "frontend" is in my config file).
I have a form, Frontend_Form_PropertySearch located at /application/modules/frontend/forms/PropertySearch.php and attempting to use it in my controller as follows:
public function searchAction()
{
$form = new Frontend_Form_PropertySearch();
$form->submit->setLabel('Search');
$this->view->form = $form;
}
However, I'm getting the following error:
Fatal error: Class 'Frontend_Form_PropertySearch' not found in /Users/Martin/Dropbox/Repositories/realestatecms/application/modules/frontend/controllers/PropertiesController.php on line 17
Where am I going wrong?
One of solutions could be adding file application/modules/frontend/Bootstrap.php and put this (similar working on one of my projects):
<?php
class Frontend_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Frontend_',
'basePath' => APPLICATION_PATH .'/modules/frontend',
'resourceTypes' => array (
'form' => array(
'path' => 'forms',
'namespace' => 'Form',
),
'model' => array(
'path' => 'models',
'namespace' => 'Model',
),
)
));
return $autoloader;
}
}
Another solution, as described by akrabat: http://akrabat.com/zend-framework/bootstrapping-modules-in-zf-1-8/
// file application.ini
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
File: /application/modules/frontend/Bootstrap.php
<?php
class Frontend_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
Second one uses default resource autoloader as described in documentation: http://framework.zend.com/manual/zh/zend.loader.autoloader-resource.html#zend.loader.autoloader-resource.module
Make sure your ini file contains these lines
resources.frontController.moduleDirectory = APPLICATION_PATH "/path/to/your/modules"
resources.modules[] =

Zend findDependentRowset An error occurred

I have three two classess
class Application_Model_Accounts extends Zend_Db_Table_Abstract
{
protected $_name = 'accounts';
protected $_dependentTables = array('Application_Model_Bugs');
}
And
class Application_Model_Bugs extends Zend_Db_Table_Abstract
{
protected $_name = 'bugs';
protected $_dependentTables = array('Application_Model_BugsProducts');
protected $_referenceMap = array(
'Reporter' => array(
'columns' => 'reported_by',
'refTableClass' => 'Application_Model_Accounts',
'refColumns' => 'account_name'
),
'Engineer' => array(
'columns' => 'assigned_to',
'refTableClass' => 'Application_Model_Accounts',
'refColumns' => 'account_name'
),
'Verifier' => array(
'columns' => array('verified_by'),
'refTableClass' => 'Application_Model_Accounts',
'refColumns' => array('account_name')
)
);
}
in index controll i am trying to run this code.
public function indexAction()
{
$accountsTable = new Application_Model_Accounts();
$accountsRowset = $accountsTable->find(1234);
$user1234 = $accountsRowset->current();
$bugsReportedByUser = $user1234->findDependentRowset('Application_Model_Bugs');
}
and on line
$bugsReportedByUser = $user1234->findDependentRowset('Application_Model_Bugs');
I am getting this error
An error occurred
Application error
I am unable to findout the problem. How to fix this problem. and is there a way to get more developer friendly error in Zend rather then just getting this message "An error occured".
I figure out the solution for this problem.
in .Htaccess file
enable the development mode by adding this line on top
SetEnv APPLICATION_ENV "development"
This will show the complete error trace.
Sorted
full working example of bugs code including db available here
http://phphints.wordpress.com/2010/06/25/zend-framework-finddependentrowset-and-findparentrow-demo/

Zend Framework: How and where to create custom routes in a 1.8+ application?

I've never created a custom route before, but I finally have a need for one. My question is: How can I create a custom route, and where should I create it? I'm using Zend Framework 1.9.6.
Here's how I did it. There may be a better way, but I think this is as simple as it gets:
# /application/Bootstrap.php
protected function _initSpecialRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute(
'verify', new Zend_Controller_Router_Route('verify/:token', array(
'controller' => 'account',
'action' => 'verify'))
);
$router->addRoute(
'arbitrary-route-name', new Zend_Controller_Router_Route('special/route/:variablename', array(
'controller' => 'defaultcontrollername',
'action' => 'defaultactionname'))
);
}
Here's where I define my custom routes:
// in bootstrap
Zend_Controller_Front::getInstance()->registerPlugin( new Prontiso_Controller_Plugin_Routes() );
...
<?php
// Prontiso/Controller/Plugin/Routes.php
class Prontiso_Controller_Plugin_Routes extends Zend_Controller_Plugin_Abstract
{
public function routeStartup( Zend_Controller_Request_Abstract $request )
{
Prontiso_Routes::addAll();
}
}
...
<?php
// Prontiso/Routes.php
class Prontiso_Routes extends Zend_Controller_Plugin_Abstract
{
public static function addAll()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
/**
* Define routes from generic to specific.
*/
/**
* Example: Simple controller/action route, guaranteed to eliminate any extra URI parameters.
*/
$router->addRoute(
'barebones', new Zend_Controller_Router_Route(':controller/:action')
);
}
}
See the Router documentation for the how. And I would make your routes in the bootstrap. Either program the routes by hand or load a configuration file.
I know there's already an accepted answer by #Andrew but I wanted to post this method that is very similar to his but I find cleaner.
If you dig into Zend_Controller_Router_Rewrite you will find an addRoutes() method which just iterates over each key and value and calls addRoute().
So here's my solution:
# /application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$loginRoute = new Zend_Controller_Router_Route('login', array('controller' => 'auth', 'action' => 'login'));
$logoutRoute = new Zend_Controller_Router_Route('logout', array('controller' => 'auth', 'action' => 'logout'));
$routesArray = array('login' => $loginRoute, 'logout' => $logoutRoute);
$router->addRoutes($routesArray);
}
}