I'm new to Zend Framework. I've been following book "ZendFramework - A Beginners Guide". in the 4th chapter, they start using Doctrine. The thing is that I'd like to use Zend's built-in functionality with database interactions.
I've searched the web and watched couple of tuts. Well I couldn't find the right (for me) solution.
in all tuts, everyone is using standard Zend's structure. I'd like to integrate my Modules. So I have a file structure like this:
application
/modules
/moduleName
/controllers
/models
/views
I have set up the routes in application.ini for controllers and views like this:
; /articles/* route
resources.router.routes.articles.route = /articles
resources.router.routes.articles.defaults.module = content
resources.router.routes.articles.defaults.controller = articles
resources.router.routes.articles.defaults.action = index
Now I tried to load-up my models so I start interacting with the tables in Mysql.
in my controller named ArticlesController.php I created the class Content_ArticlesController and in there I have:
public function indexAction()
{
$model = new Content_ArticlesModel();
$this->view->modelHi = $model->there;
$this->view->hello = 'Hello there';
}
As you might guess Content_ArticlesModel is my class that extends Zend_Db_Table_Abstract class. in there I have a public function modelHi() that I'd like to call from my Controller to pass the data from the model to the view.
Can someone help me do this in the right way? how should I correctly load-up my models for my modules? and maybe you could link some good article on working with database based on some example.
EDIT: Ok, I had this really studip error in my Controller but it's ok. Anyways the problem is that when Zend loads-up my controller it displays: Fatal error: Class 'Content_ArticlesModel' not found in S:\xampp\htdocs\testsite\application\modules\content\controllers\ArticlesController.php on line 12
The issue is my Zend can't locate/doesn't know where to find the models of my module that I'm trying to reach from my controller.
I want to know how should I make Zend see and use my models?
You should make configuration in application.ini file for modeule and view
resources.modules =
resources.view[] =
Also you should make bootstrap file inside your module
class Content_Bootstrap extends Zend_Application_Module_Bootstrap {
}
Also you should ensure that the name of your modulel folder is small letter "content"..
After making these configuration, you will be able to call model inside controller...
Why not just call your public modelHi() method in your controller
Update:
If your module name is 'content' , then your model should be located
application/modules/content/models/ArticlesModel.php
then do the following
public function indexAction()
{
$model = new Content_Model_ArticlesModel(); //name of class
$this->view->modelHi = $model->modelHi();
$this->view->hello = 'Hello there';
}
Related
I'm working in a Zend Framework based application (Shopware).
I add a template dir in my controller like this:
class Shopware_Controllers_Backend_Pricify extends Shopware_Controllers_Backend_ExtJs
{
public function init()
{
$this->View()->addTemplateDir(dirname(__FILE__) . "/../../Views/backend/");
parent::init();
}
}
But somehow, smarty always looks in the (not existing) part of the controller action:
Unable to load template snippet 'backend/mycontroller/model/main.js' in 'snippet:string:{include file="backend/pricify/model/main.js"} in Smarty/sysplugins/smarty_internal_templatebase.php on line 128
The Controller works over loading via ext js, but I do not see that this is a problem. When I var_dump template directories, the correct dir is included. I debugged the code far into smarty, but never found the part, where the directories are checked.
I'm aware, that this may be a problem within the software stack, but since I do not know where to search, I ask here. If I need to post additional data, please tell me.
I found, that the problem was that shopware extends CamelCase to camel_case folders.
How can I make every module have it's own layouts directory?
I.e. when I don't have any modules my layout entry in config file looks like this:
resources.layout.layoutPath = APPLICATION_PATH "/layouts"
I try entering i.e.
; Layout directory for admin module
admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts"
Where admin is module name; but it doesn't work. For some strange reason ZF looks for module layouts in /module/admin/views/scripts directory.
I also have a separate module.ini config file for every module as per this tutorial, alas layout path there gets ignored as well. Also I've been trying to follow this modules layout tutorial but it didn't work, I guess due to differences in ZF versions (tutorial is rather old). So I don't know what else to do
Using plugin from the tutorial you are talked about:
class My_Controller_Plugin_RequestedModuleLayoutLoader extends Zend_Controller_Plugin_Abstract {
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$config = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
$moduleName = $request->getModuleName();
if (isset($config[$moduleName]['resources']['layout'])) {
Zend_Layout::startMvc($config[$moduleName]['resources']['layout']);
}
}
}
application.ini
resources.frontController.plugins.layoutloader = My_Controller_Plugin_RequestedModuleLayoutLoader
module.ini:
resources.layout.layout = "Admin"
resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts/scripts"
Working fine.
A slightly alternate method to Ololo recommendation (which is a great way to do it)..
class YourApp_Controller_Plugin_Modulelayout extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$module = $request->getModuleName();
if ($module != 'default')
{
if (file_exists(APPLICATION_PATH . '/layouts/' . $module . '.html')) {
Zend_Layout::getMvcInstance()->setLayout($module);
}
}
}
}
Place this controller plugin in /library/YourApp/Controller/Plugin/Modulelayout.php
Then save your module layouts as the module name in your layouts folder (E.g., /layout/admin.phtml). If it does not find a layout for that module, it will default back to layout.phtml or whatever you originally set it to.
Have a look at this gist - https://gist.github.com/891384
This uses a combination of
Action helper to inspect the requested module and given a matching configuration, change the layout's layout and layoutPath properties in the preDispatch hook
Application resource plugin to capture module layout options, inject them into the above helper and add it to the helper broker
Happened to me too I got around it by using this line in my controller (I created a init function)
Zend_Layout::startMvc(array('layoutPath' => APPLICATION_PATH . '/modules/admin/layouts'));
As of Zend Framework 1.12 (Haven't tested it on previous releases):
Create your modules
Initialize the layout in your prefered fashion. For example in application.ini as zend tools does it:
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
Create a layout inside each module with the default layout name inside the modules path to "views/scripts/" for example "application/modules/default/views/scripts/layout.phtml"
Don't forget to create one for the default module as it will be your fallback layout!
DO NOT create the default layout inside /application/layouts/scripts or this won't work
You are ready to run!
When Zend_Layout doesn't find the default layut it will look into the modules folders for it.
If you need some extra tweaking you may create a plugin and assign it to the layout object itself. For example, inside application.ini:
resources.layout.pluginClass = "MyLibrary_Controller_Plugin_Layout"
...or in the Bootstrap:
Zend_Layout::getMvcInstance()->setPluginClass("MyLibrary_Controller_Plugin_Layout");
Cheers!
I am learning how to use Zend framework and realise that the action helper is something that would be useful.
I have set up a default installation of Zend on my machine, but I dont know where the helper file needs to go, what I need to put in the bootstrap file and how I use it. Can anyone point me in the right direction please - the ZF user guide is not to clear to me.
Thanks
John
Two thoughts for where to place your custom action-helpers:
In a separate, custom library
In the folder application/controllers/helpers
These ideas are not exclusive. Functionality that is general enough to work in multiple projects should probably be pulled into a separate library. But for functionality that is application-specific, there is an argument that it could be somewhere in the application folder.
#Jurian has already described the "separate-library" approach. For app-specific helpers, you can do as follows:
For a helper called myHelper, create a class Application_Controller_Helper_MyHelper in the file application/controllers/helpers/MyHelper.php. In Bootstrap, you have something like:
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Application',
'basePath' => APPLICATION_PATH,
));
Zend_Controller_Action_HelperBroker::addPath(
APPLICATION_PATH . '/controllers/helpers',
'Application_Controller_Helper_');
return $autoloader;
}
Then your helper can be invoked in a controller by using:
$this->_helper->myHelper;
As you can see, this presumes you are using appNamespace 'Application'. If not, you can (must!) modify your class names to accommodate your circumstance.
Cheers!
You can place action helpers in your own library. Besides library/Zend where all the Zend stuff is around, you can create a library/MyLibrary folder (MyLibrary is arbitrary chosen) and put the action helpers there.
A good place is the library/MyLibrary/Controller/Action/Helper folder you need to create and place your action helper there (i.e. Navigation.php). In this file, create the class MyLibrary_Controller_Action_Helper_Navigation.
The next step is to add the action helper to the HelperBroker of the Zend Framework during bootstrap. Therefore, create a new method in your Bootstrap.php file and add this function:
protected function _initActionHelpers ()
{
Zend_Controller_Action_HelperBroker::addHelper(
new MyLibrary_Controller_Action_Helper_Navigation()
);
}
One last remark is you need to configure the use of this library by adding this rule to your application.ini:
autoLoaderNameSpaces[] = "MyLibrary_"
You can do this through your application.ini file like so
resources.view[] =
resources.view.helperPath.Default_View_Helper_ = APPLICATION_PATH "/views/helpers/"
Then in your views/helpers path you can create a file like Time.php. This file would contain the following code:
<?php
class Default_View_Helper_Time extends Zend_View_Helper_Abstract
{
public function time()
{
$date = new Zend_Date();
return $date->get(Zend_Date::TIME_MEDIUM);
}
}
?>
To use this in your view scripts you would use
<?=$this->time()?>
Which would display the current time using your new View_Helper
You can avoid having to register your action helper namespace and path within the Bootstrap.php by declaring them in the application.ini instead like so:
resources.frontController.actionHelperPaths.My_Controller_Action_Helper = APPLICATION_PATH "/controllers/helpers"
Simply replace My_Controller_Action_Helper with your desired namespace, and modify the helpers directory path accordingly.
The helper can be initialized the same way:
$this->_helper->myHelper;
As mentioned by the docs, registering the prefix or path of the helpers is usually preferred because helpers would not be initialized until they are called like in the snippet above.
Of course, instantiating and passing helpers to the broker is a bit
time and resource intensive, so two methods exists to automate things
slightly: addPrefix() and addPath().
http://framework.zend.com/manual/1.12/en/zend.loader.pluginloader.html
Adding the config entry to the application.ini follows the same suggested pattern.
I am making a customer portal application using ZF. And the portal needs to work for different company brands. So I need to use all of the same backend code/controllers/etc, but dynamically change the view directory based off of the hostname.
Right now my view directory structure looks something like this:
/application/views/scripts/brand1/
/application/views/scripts/brand1/index/index.phtml
/application/views/scripts/brand1/error/error.phtml
/application/views/scripts/brand2/
/application/views/scripts/brand2/index/index.phtml
/application/views/scripts/brand2/error/error.phtml
/application/views/scripts/brand3/
/application/views/scripts/brand3/index/index.phtml
/application/views/scripts/brand3/error/error.phtml
and so on.
I am using the addScriptPath() function in bootstrap.php like so
protected function _initView()
{
$view = new Zend_View();
$view->doctype('XHTML1_STRICT');
$view->env = APPLICATION_ENV;
$view->addScriptPath(APPLICATION_PATH . '/views/scripts/brand1');
$view->addHelperPath(APPLICATION_PATH . '/views/helpers');
...
}
However when this is run, it is looking for all views using /views/scripts/brand1/(action).phtml instead of looking for views using the correct scheme /view/scripts/brand1/(controller)/(action).phtml
tl;dr is it possible to dynamically choose the view directory and have it work like the default /views/scripts/(controller)/(action).phtml behavior?
I knew I would find the answer after I posted here. In case anyone else encounters the same problem, the solution was using:
$view->setBasePath(APPLICATION_PATH . '/views/brand1');
And then modifying the directory structure to:
/application/views/brand1/scripts/...
My application setup with 2 modules admin and default
I test the controller which works fine on modules
but the models doesnt work
I created a model application\modules\admin\models\User.php
<?php
class Admin_Model_User{
}
inside the controller
$user = new Admin_Model_User();
Fatal error: Class 'Admin_Model_User'
not found
Essentially, you need 2 lines in the application.ini file;
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
Then, for each module, you need a module bootstrap file:
File: myproject/application/modules/{modulename}/Bootstrap.php
<?php
class {Modulename}_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
(Yes, it is an empty class.)
Further details are at http://akrabat.com/zend-framework/bootstrapping-modules-in-zf-1-8/.
Configure an autoloader so that the framework can map your class prefix Admin_Model to the corresponding source path. This is not done automatically.
I suggest reading the part on models of the Zend Framework Quickstart, which explains in detail how to do this.
Are you using an autoloader?
If you do you should change the class name (or path) to reflect the path (or class name)
Models <> Model
You should have
Admin_Model_User in admin/model/user.php
or
Admin_Models_User in admin/models/user.php.