zend rest controller routing to specific controller - zend-framework

I am fairly new to zend framework. I have been trying to write RESTful controller using Zend_Rest_Controller. I built one from a good tutorial
http://www.techchorus.net/create-restful-applications-using-zend-framework
It works perfectly. So I went ahead added it to the existing zend application. I just wanted one controller to be RESTful so did the necessary changes in the bootstrap.
protected function _initRestRoute()
{
$this->bootstrap('frontController');
$frontController = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($frontController, array() , array('default' => array('MyserviceController')));
$frontController->getRouter()->addRoute('rest', $restRoute);
}
the server throws up a 500 internal server error when i try to access the service using the url http://localhost/projectname/public/index.php/myservice which is supposed to invoke the index method from the MyserviceController. The application has the following folder structure
application/
configs/
application.ini
controllers/
IndexContoller
OneController
TwoController
Myservice
forms
layouts
models
modules/
app1module
app2module
app3module
views
Bootstrap.php
this is the application.ini for the project
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "America/New_York"
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
includePaths.helpers = APPLICATION_PATH "/views/helpers"
;Module support
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.defaultModule = "default"
resources.modules[] = ""
resources.db.adapter = PDO_MYSQL
resources.db.params.host = ********
resources.db.params.username = *********
resources.db.params.password = *********
resources.db.params.dbname = *********
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
and this the code for MyserviceController.php
<?php
class MyserviceController extends Zend_Rest_Controller
{
public function init()
{
parent::init();
$this->_helper->viewRenderer->setNoRender(true);
}
public function indexAction() {
$this->getResponse()->setBody('Hello World');
$this->getResponse()->setHttpResponseCode(200);
}
public function getAction() {
$this->getResponse()->setBody('Foo!');
$this->getResponse()->setHttpResponseCode(200);
}
public function postAction() {
$this->getResponse()->setBody('resource created');
$this->getResponse()->setHttpResponseCode(200);
}
public function putAction() {
$this->getResponse()->setBody('resource updated');
$this->getResponse()->setHttpResponseCode(200);
}
public function deleteAction() {
$this->getResponse()->setBody('resource deleted');
$this->getResponse()->setHttpResponseCode(200);
}
}
?>

In "localhost/projectname/public/index.php/myservice" why you using index.php, why not just /index/ ?
ZF's default URL structure is the "http://hostname/controller/action/parametes"

Related

Why does the application work this way?

variables from controller do not appear in a view and view-helpers do not work:
for example I define a variable in controller:
$this->view->title = "Title";
and print it in a view:
echo $this->title;
The contents of the variable "titles" doesn't appear in the page.
Here is my bootstrap file:
use ZFBootstrap\View\Helper\Navigation\Menu;
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload() {
require_once '../application/views/helpers/LoggedInAs.php';//need to include a view-helper
require_once '../application/plugins/AuthPlugin.php';
require_once '../application/configs/configAcl.php';
$frontController = Zend_Controller_Front::getInstance();
$frontController->registerPlugin($auth = new AuthPlugin());
$frontController->getRouter()->addDefaultRoutes();
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
$this->bootstrap('layout');
$layout=$this->getResource('layout');
$view=$layout->getView();
$config = new Zend_Config_Xml(APPLICATION_PATH. '/configs/navigation.xml', 'nav');
$container = new Zend_Navigation($config);
$view->registerHelper(new Menu(), 'menu');
$view->navigation($container);
Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
$auth = Zend_Auth::getInstance();
$identity = $auth->getIdentity();
$role = ($auth->hasIdentity() && !empty($auth->getIdentity()->role))
? $auth->getIdentity()->role : 'guest';
Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($role);
$view->navigation()->menu()->render();
$view->navigation()->menu()->setMinDepth(1)->setUlClass('nav');
}
If instead I use this code
use ZFBootstrap\View\Helper\Navigation\Menu;
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload() {
require_once '../application/plugins/AuthPlugin.php';
require_once '../application/configs/configAcl.php';
$frontController = Zend_Controller_Front::getInstance();
$frontController->registerPlugin($auth = new AuthPlugin());
$frontController->getRouter()->addDefaultRoutes();
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
/*$this->bootstrap('layout');
$layout=$this->getResource('layout');
$view=$layout->getView();*/
$config = new Zend_Config_Xml(APPLICATION_PATH. '/configs/navigation.xml', 'nav');
$container = new Zend_Navigation($config);
/*$view = Zend_Layout::startMvc()->getView();
$view->registerHelper(new Menu(), 'menu');
$view->navigation($container);//->setAcl($acl)->setRole($auth->getStorage()->read()->role);
*/Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
$auth = Zend_Auth::getInstance();
$identity = $auth->getIdentity();
$role = ($auth->hasIdentity() && !empty($auth->getIdentity()->role))
? $auth->getIdentity()->role : 'guest';
Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($role);
/*$view->navigation()->menu()->render();
$view->navigation()->menu()->setMinDepth(1)->setUlClass('nav');*/
}
everything works exept navigation. The question is, why my navigation doesn't work and how can I fix it.
To begin with your Bootstrap.php is an utter mess. Start with some basic organization.
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
/**
* initialize the session
*/
protected function _initsession()
{
Zend_Session::start();
}
/**
* initialize the registry and assign application.ini to config namespace of Zend_Registry
*/
protected function _initRegistry()
{
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
}
When you want to manipulate the view object in your bootstrap you need to make sure to return the new view:
/**
* initialize the view and return it
* #return \Zend_View
*/
protected function _initView()
{
//Initialize view
$view = new Zend_View();
//add custom view helper path
$view->addHelperPath('/../library/My/View/Helper');
//set doctype for default layout
$view->doctype(Zend_Registry::get('config')->resources->view->doctype);
//set head meta data
$view->headMeta()->setCharset(Zend_Registry::get('config')->resources->view->charset);
//set css includes
$view->headlink()->setStylesheet('/bootstrap/css/bootstrap.min.css');
$view->headLink()->appendStylesheet('/css/main.css');
//add javascript files
$view->headScript()->setFile('/bootstrap/js/jquery.min.js');
$view->headScript()->appendFile('/bootstrap/js/bootstrap.min.js');
//add the view to the view renderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer');
$viewRenderer->setView($view);
//Return it, so that it can be stored by the bootstrap
return $view;
}
As far as navigation is concerned. You can initialize your navigation container in the bootstrap but the actual view components will typically bet setup in the layout:
/**
* The bootstrap part as example, yours will likely be similar but different
*/
protected function _initNavigation() {
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/site.xml');
$container = new Zend_Navigation($config);
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Navigation', $container);
}
Then in your layout file you may implement the navigation similar to:
<div id="nav">
<?php echo $this->navigation()->menu()
->renderMenu(null, array(
'minDepth' => null,
'maxDepth' => 1,
'onlyActiveBranch' => TRUE
)) ?>
</div>
One thing to keep in mind: The options we getting when we call $this->getOptions() in the _initRegistry() method come from the application.ini file. A large number of options are often initialize here.
//application.ini as example your settings will be somewhat different
[production]
;-------------------------------------------------------------------------------
;PHP
;-------------------------------------------------------------------------------
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
;-------------------------------------------------------------------------------
;Paths and Namespaces
;-------------------------------------------------------------------------------
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
autoloaderNamespaces[] = "My_"
;-------------------------------------------------------------------------------
;Front Controller
;-------------------------------------------------------------------------------
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.frontController.moduleControllerDirectoryName = "controllers"
resources.frontController.params.prefixDefaultModule = ""
resources.modules = ""
resources.frontController.baseurl = http://example.com
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
;-------------------------------------------------------------------------------
;plugins
;-------------------------------------------------------------------------------
pluginPaths.My_Application_Resource = APPLICATION_PATH "/../library/My/Resource"
resources.frontController.actionhelperpaths.My_Controller_Action_Helper = APPLICATION_PATH "/../library/My/Controller/Action/Helper"
;-------------------------------------------------------------------------------
;View Settings
;-------------------------------------------------------------------------------
resources.view[]=
resources.view.charset = "UTF-8"
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.language = "en"
;-------------------------------------------------------------------------------
;Database Settings
;-------------------------------------------------------------------------------
resources.db.adapter = "pdo_Mysql"
resources.db.params.username = "user"
resources.db.params.password = "xxxxxxx"
resources.db.params.dbname = "dbname"
resources.db.params.charset = "utf8"
resources.db.isDefaultTableAdapter = true
resources.db.params.profiler = true
;-------------------------------------------------------------------------------
;Layouts
;-------------------------------------------------------------------------------
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
If your page is blank and you think it shouldn't be. Set your environment to 'development' or just change the error display values from '0' to '1' in the 'production' section. If you do this the errors will display.
Good Luck

zend framework: plugin controller doesn't work

I want to get use of Controller plugins in Zend Framework.
I've created a folder "plugins" in application folder and a file Test.php inside of it. It's content:
<?php
class Plugin_Multilanguage extends Zend_Controller_Plugin_Abstract{
public function preDispatch(){
echo 'The plugin is working';
}
}
in the Bootstrap file, I have added function:
protected function _initAutoload(){
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_Multilanguage());
}
And it doesn't work. I don't get even any kind of error or worning. Just a blank page. I have noticed that there plenty of similar topics, I've read all of them, but I'm still confused with that simple issue. I use Zend Framework 1.11. Please help.
I got following error:
Fatal error: Class 'Plugin_Multilanguage' not found in C:\zendSites\moduleDeveloper\application\Bootstrap.php on line 13
My application.ini file content:
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = ""
resources.frontController.params.displayExceptions = 0
resources.frontController.params.prefixDefaultModule = "1"
resources.router.routes.defaultmodule.type = Zend_Controller_Router_Route_Module
resources.router.routes.defaultmodule.abstract = On
resources.router.routes.defaultmodule.defaults.module = "system"
resources.router.routes.language.type = Zend_Controller_Router_Route
resources.router.routes.language.route = ":language"
resources.router.routes.language.reqs.language = "^(fr|en)$"
resources.router.routes.language.defaults.language = "en"
resources.router.routes.default.type = Zend_Controller_Router_Route_Chain
resources.router.routes.default.chain = "language, defaultmodule"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
I've tried to add one by one following resources:
pluginPaths.Plugins = APPLICATION_PATH "/plugins"
autoloaderNamespaces[] = "plugins"
resources.frontController.plugins[] = "Plugin_Multilanguage"
pluginPaths.Plugins = APPLICATION_PATH "/plugins"
autoloaderNamespaces[] = "plugins"
Only the error message is changing:
Fatal error: Class 'Plugin_Multilanguage' not found in C:\zend\library\Zend\Application\Resource\Frontcontroller.php on line 117
What am I doing wrong?
The chances are that your plugin path is not readable by the application:
APPLICATION_PATH "/plugins"
These kind of paths are not readable by default for class code in ZF.
Try moving your plugins to an Application library:
/library
/Application
/Plugin
Multilanguage.php
Update your code to reflect this:
<?php
class Application_Plugin_Multilanguage extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(){
echo 'The plugin is working';
}
}
And update the bootstrap:
protected function _initAutoload()
{
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Application_Plugin_Multilanguage());
}

Zend Route - Remove parameter name from URL

I'm starting developing using Zend Framework and I have a question about routes. Is there a way to instead of having a URL like this:
www.mysite.com/newsletter/groups/edit/id/1
Have this:
www.mysite.com/newsletter/groups/edit/1
(without the parameter name id)
I already put this code to declare a custom route in my BootStrap file:
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
/* Edit Groups */
$route = new Zend_Controller_Router_Route('groups/edit/:group_id',array('controller' => 'groups','action' => 'edit'));
$router->addRoute('group_edit', $route);
return $router;
}
Then in my view file I use this to echo the URL:
<?=$group->getName()?>
And the url is echoing the way I want:
Group 1
This is my application.ini:
[production]
appnamespace = "Application"
; Debug output
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
resources.frontController.params.displayExceptions = 0
;PHP Setings
phpsettings.date.timezone = "America/Sao_Paulo"
; Include path
includePaths.models = APPLICATION_PATH "/models"
includePaths.application = APPLICATION_PATH
includePaths.library = APPLICATION_PATH "/../library"
; Bootstrap
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
; Front Controller
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.env = APPLICATION_ENV
resources.frontController.actionHelperPaths.Action_Helper = APPLICATION_PATH "/views/helpers"
resources.frontController.moduleControllerDirectoryName = "actions"
;resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.defaultControllerName = "index"
resources.frontController.defaultAction = "index"
resources.frontController.defaultModule = "default"
;resources.frontController.baseUrl = "/newsletter"
;resources.frontController.returnresponse = 1
; Layout
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
; Views
resources.view.helperPath = APPLICATION_PATH "/views/helpers"
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views"
resources.view.scriptPath.Default = APPLICATION_PATH "/views/scripts"
resources.view.doctype = "HTML5"
resources.view.contentType = "text/html;charset=utf-8"
resources.view.helperPathPrefix = "Views_Helpers_"
resources.view.filterPathPrefix = "Views_Filters_"
resources.db.adapter = "PDO_SQLITE"
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
resources.frontController.throwExceptions = true
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.dbname = "newsletter"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.isDefaultTableAdapter = true
resources.db.params.charset = utf8
My complete Boostrap file:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
/**
* Init Autoloader
*/
protected function _initAutoload()
{
$loader = Zend_Loader_Autoloader::getInstance();
$loader->setFallbackAutoloader(true);
}
/**
* Adiciona alguns routers
*/
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
/* Edit Groups*/
$route = new Zend_Controller_Router_Route('/groups/edit/:groupId',array('controller' => 'group','action' => 'edit'));
$router->addRoute('group_edit', $route);
return $router;
}
}
My index.php (inside public) file:
<?php
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
The problem is, when I click to open this page (the edit group page) I get this error:
Fatal error: Uncaught exception 'Zend_Controller_Router_Exception' with message 'group_id is not specified' in C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\Controller\Router\Route.php:354 Stack trace: #0 C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\Controller\Router\Rewrite.php(470): Zend_Controller_Router_Route->assemble(Array, true, true) #1 C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\View\Helper\Url.php(49): Zend_Controller_Router_Rewrite->assemble(Array, NULL, true, true) #2 [internal function]: Zend_View_Helper_Url->url(Array, NULL, true) #3 C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\View\Abstract.php(350): call_user_func_array(Array, Array) #4 C:\xampp\htdocs\fuhrmann\newsletter\application\layouts\scripts\layout.phtml(22): Zend_View_Abstract->__call('url', Array) #5 C:\xampp\htdocs\fuhrmann\newsletter\application\layouts\scripts\layout.phtml(22): Zend_View->url(Array, NULL, true) #6 C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\View.php(108): include('C:\xampp\htdocs...') #7 C:\xampp\htdocs\fu in C:\xampp\htdocs\fuhrmann\newsletter\library\Zend\Controller\Plugin\Broker.php on line 336
Inside my EDIT action I can do a var_dump in all request params to see if the groupId is set, and yes, it is!
array(3) { ["groupId"]=> string(3) "555" ["controller"]=> string(6) "groups" ["action"]=> string(6) "edit" }
I already have searched for a lot of answers here and by the way, I found a question with an answer but no solution for me.
Thanks!
Ok, solved!
What I did was to add a default value (NULL) to the group_id, so in my Bootstrap file I have this now:
$route = new Zend_Controller_Router_Route('/groups/edit/:group_id',array('controller' => 'groups','action' => 'edit', 'group_id' => NULL));
Thanks #philien!
You can try by removing the underscore :
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
/* Edit Groups */
$route = new Zend_Controller_Router_Route('groups/edit/:groupId',array('controller' => 'groups','action' => 'edit'));
$router->addRoute('group_edit', $route);
return $router;
}
And after :
<?=$group->getName()?>
Underscore are bad to use in this case, I choose to put names in CamelCased.

Zend autoloading configurations in bootstrap vs application.ini

Given:
1.
bootstrap:
$autoloader = Zend_Loader_Autoloader::getInstance();// Zend_Loader_Autoloader
$autoloader->registerNamespace('Ntk_');
equals to
application.ini:
autoloaderNamespaces[] = "Ntk_"
2.
bootstrap:
$pluginLoader = $this->getPluginLoader();// Zend_Loader_PluginLoader
$pluginLoader->addPrefixPath('My_Resource', APPLICATION_PATH . "/appResourcePlugins");
equals to
application.ini:
pluginPaths.My_Resource = APPLICATION_PATH "/appResourcePlugins"
3.
bootstrap:
$moduleAutoloader = $this>getResourceLoader();//Zend_Application_Module_Autoloader
$moduleAutoloader->addResourceType('need', 'needs', 'Needs');
equals to
application.ini:
???
What is the application.ini method for configuring Zend_Application_Module_Autoloader ?
(...if it exists)
I use zend framework version 1.11.10
Maybe this example will help you:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initApplication()
{
$this->bootstrap('frontcontroller');
$front = $this->getResource('frontcontroller');
$front->addModuleDirectory(dirname(__FILE__) . '/modules');
}
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
}
Here is application.ini
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.modules = ""
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.view[] =
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
Reference:someone had the same question and got his answer here

Problems integrating Zend Framework and Doctrine

I've done this before on another machine and the setup is almost identical. However, its just not working on the new machine. When I try and get Doctrine to generate my models, its putting them in the wrong place. Its creating an extra "Model" directory both for the base classes as well as the regular classes. I'm using Doctrine 1.2.3 with ZF 1.11.0. Here is my ZF application.ini:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "America/Denver"
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = ""
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.view[] =
autoloaderNamespaces[] = "Doctrine_"
autoloaderNamespaces[] = "App_"
doctrine.connection_string = "mysql://root#localhost/mydb"
;doctrine.cache = true
doctrine.data_fixtures_path = APPLICATION_PATH "/doctrine/data/fixtures"
doctrine.sql_path = APPLICATION_PATH "/doctrine/data/sql"
doctrine.migrations_path = APPLICATION_PATH "/doctrine/migrations"
doctrine.yaml_schema_path = APPLICATION_PATH "/doctrine/schema"
doctrine.models_path = APPLICATION_PATH "/models"
doctrine.generate_models_options.pearStyle = true
doctrine.generate_models_options.generateTableClasses = true
doctrine.generate_models_options.generateBaseClasses = true
doctrine.generate_models_options.baseClassPrefix = "Base_"
doctrine.generate_models_options.baseClassesDirectory =
doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.classPrefix = "Model_"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
Here is my Doctrine function from my ZF Bootstrap.php file:
protected function _initDoctrine()
{
$this->getApplication()->
getAutoloader()->
pushAutoloader(array('Doctrine_Core', 'autoload'));
$config = $this->getOption('doctrine');
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_');
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR);
$manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
$manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);
if (isset($config['cache']) && $config['cache'] == true) {
$cacheDriver = new Doctrine_Cache_Apc();
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
}
$connection = $manager->openConnection($config['connection_string'], 'doctrine');
$connection->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
$connection->setCharset('utf8');
return $connection;
}
And here is my doctrine.php command line script:
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path()
)));
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->getBootstrap()->bootstrap('doctrine');
$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
So, I'm expecting my models directory to end up like this:
models
Base
-Class1.php
-Class2.php
-Class1.php
-Class1Table.php
-Class2.php
-Class2Table.php
where the base class class names are formatted like Model_Base_Class1 and the regular class names are Model_Class1 (in the actual php files themselves). Instead, what I'm getting is:
models
Base
Model
-Class1.php
-Class2.php Model
-Class1.php
-Class1Table.php
See the extra Model directories? What is causing this? I've tried messing with the doctrine.generate_models_options.classPrefix option in my ini as well as the similar setting in my Bootstrap.php file - $manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_');
Please help me!
Well, I'm not so smart once again. :( All it was, was a misconfiguration of my schema.yaml file. I had put Model_User as the model name instead of just User! The fixtures file does require it to be Model_User though. Problem solved.