I can pass value from controller to view with this =>
$this->view->objvalue = $value;
But how can i pass value to layout.phtml ?
I want to have a link to edit table. So i have to make the link with 'id'=>$getvaluefromsomewhere->id. How can i do this in zend framework ?
Where would i put this file (My_Layout_Plugin.php) ?
class My_Layout_Plugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
$view->whatever = 'foo';
}
}
Sending variables to the layout in Zend Framework
put it in library/My/Layout/Plugin.php and add this in application.ini
resources.frontController.plugins.layout = "My_Layout_Plugin"
See here Zend framework 1.8 recommended way to register a plugin?
Related
I am having problem with adding custom routing into my application. Zend Framework 1:
I have already working application with default Zend routing. I have a lot of controllers and for some of them I want to use friendly urls, for example:
domain.com/cities/edit/id/3
to
domain.com/cities/edit/abu-dhabi
There are some controllers to switch into that url (in that format).
I tried to configure it by ini file:
resources.router.routes.slugurl.route = /:controller/:action/:slug
And also by Bootstrap method:
protected function _initSlugRouter()
{
$this->bootstrap('FrontController');
$router = $this->frontController->getRouter();
$route = new Zend_Controller_Router_Route('/:controller/:action/:slug/',
array(
'controller' => 'slug',
'action' => 'forward',
'slug' => ':slug'
)
);
$router->addRoute('slug',$route);
}
The main problem - with ini configuration - request goes directly to controller city (not slug). Second one (bootstrap) - also executes the city controller, but the default routing not working as it was, I am not able to execute:
domain.com/cities/ but domain.com/cities/index/ works. The error without action defined:
Action "forward" does not exist and was not trapped in __call()
I can monitor the "slug" in controllers/move check into some library, but I would like to do it only with routing - that way looks much better for me.. Please, how to solve that issue?
edit with summary of solution
Thank you Max P. for interesting. I solved it at last - in last 2 minutes:) I had wrong rules at application.ini with routing. Right now it is defined that:
resources.router.routes.slugurl.route = /:controller/:action/:slug
resources.router.routes.slugurl.defaults.controller = :controller
resources.router.routes.slugurl.defaults.action = :action
resources.router.routes.slugurl.defaults.slug = :slug
The controller is defined like that:
class CitiesController extends Crm_Abstract
The Crm_Abstract has code:
<?php
class Crm_Abstract extends Zend_Controller_Action {
public function __construct(\Zend_Controller_Request_Abstract $request, \Zend_Controller_Response_Abstract $response, array $invokeArgs = array()) {
$params = $request->getParams();
if (!empty($params['slug'])) {
$modelSlug = Crm_Helper_Slug::getInstance();
$parameters = $modelSlug->redirectRequest($request);
if ($parameters !== false) {
foreach ($parameters as $key => $value) {
$request->setParam($key, $value);
}
}
}
parent::__construct($request, $response, $invokeArgs);
}
public function __call($methodName, $args) {
parent::__call($methodName, $args);
}
}
The slug helper get parameters for slug at defined controller. The url to access the slug is defined/calculated in place of $this->url - to $this->slug and there rest of code is done in slug helper. I am not sure if my solution is 100% sure, because that is my first time with routing.
The major thing is that - I just needed to change Zend_Controller_Action to Crm_Abstract which extends the Zend_Controller_Action. Single line - no additional custom code in each controller.
I created two route rules:
resources.router.routes.slugurl.type = "Zend_Controller_Router_Route"
resources.router.routes.slugurl.route = /:controller/:action/:slug/
resources.router.routes.slugurl.defaults.module = "default"
resources.router.routes.slugurl.defaults.controller = :controller
resources.router.routes.slugurl.defaults.action = :action
resources.router.routes.slugurl.defaults.slug = :slug
resources.router.routes.plain1.route = "/:controller/:action/"
resources.router.routes.plain1.defaults.module = "default"
resources.router.routes.plain1.defaults.controller = "index"
resources.router.routes.plain1.defaults.action = "index"
And for now that works fine. Bad thing - I don't understand routing (htaccess rules), but the time spent on that problem and solve it is better than modify all controllers...
My zend app is created, everything seems to be in order but every time I try to do something like:
$accProducts = new Application_Models_AccProductsMapper();
Only get:
Warning: include_once(Application/Models/AccProductsMapper.php): failed to open stream: No such file or directory in /home/blah/blah/blah/Loader.php on line 148
however, the AccProductsMapper.php file do exist in such directory, directories within the zend app are all lowercase tough.
I've spend a lot of time looking for something to solve this issue with no good results at all.
Bootstrap.php
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctype()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');
}
protected function _initAutoload()
{
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace(array('App','My_'));
return $moduleLoader;
}
}
The standard Zend_Loader_Autoloader_Resource class added to each module looks for models with the class prefix <ModuleNamespace>_Model_ in <module-directory>/models.
For the default module, the namespace is defined in your config's appnamespace property (defaults to Application). The directory is typically application.
To summarize, create your default module model classes in application/models with class prefix Application_Model_, eg
<?php
// application/models/AccProductsMapper.php
class Application_Model_AccProductsMapper
{
// etc
As for your _initAutoload() method, I can't tell what you're doing with that module loader and would advise you don't need it at all. You can register PEAR style namespaces in your config file, eg
autoloadernamespaces.App = "App_"
autoloadernamespaces.My = "My_"
I have custom front controller plugin that takes some options.
At this time I load it (plugin) in application.ini file like this:
resources.frontController.plugins.DynamicLayout = "My_Controller_Plugin_DynamicLayout"
At this time I just have option.ini file and then use zend_config to import it.
Is there a way to specify plugin options from ZEND's primary application.ini file?
Maybe something like this?:
resources.frontController.plugins.DynamicLayout.test = "test_value"
I use something like this to pass info to my layouts using bootstrap.
This example is for an application that runs on different domains, thus different layouts. (and has a separate version for MSIE). Each domain as a separate application.ini
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initAutoload() {
return new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH
));
}
// <snip> more _initMethods: Loggers, ACLs, ViewHelpers, etc. </snip>
/**
* Setup dynamic layout plugin
*
* #return Zend_Controller_Plugin_Broker
*/
protected function _initFrontControllerLayoutPlugin() {
// Ensure the front controller is initialized
$this->bootstrap('FrontController');
$front = $this->getResource('FrontController');
$this->bootstrap('layout');
$layout = $this->getResource('layout');
// Set our Front Controller Plugin
// !!!! right here I pass values to the layout
// !!!! example layoutName, but you could pass anything you want...
$plugin = new Plugin_DynamicLayout($layout, $this->getOption('layoutName'));
return $front->registerPlugin($plugin);
}
}
The layout Handler:
<?php
class Plugin_DynamicLayout extends Zend_Controller_Plugin_Abstract {
private $layoutName;
public function __construct(Zend_Layout $layout, $layoutName) {
$this->layout = $layout;
$this->layoutName = $layoutName;
}
public function preDispatch(Zend_Controller_Request_Abstract $request) {
$layoutName = $this->layoutName;
if (false !== strpos($request->getHeader('User-Agent'), 'MSIE')) {
$layoutName = $layoutName . '-ie';
}
$this->layout->setLayout($layoutName);
}
}
The application.ini:
[production]
layoutName = "Some_File_Name"
I'm trying to write controller plugin to check authentication.
I created class of plugin, put in Application directory, Application.php and registered in Bootstrap.php. But there is an error: Fatal error: Class 'Authentication' not found.
Where does Zend Framework look for plugins, how to tell it where it is?
//Application/Authentication.php
class Authentication extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
return;
}
self::setDispatched(false);
// handle unauthorized request...
}
}
//bootstrap
protected function _initAutoloader()
{
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'basePath' => APPLICATION_PATH,
'namespace' => ''));
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('Common_');
return $moduleLoader;
}
protected function _initPlugins()
{
$controller = Zend_Controller_Front::getInstance();
$controller->registerPlugin(new Authentication());
$controller->dispatch();
}
Thank you.
I know the question is really old, but I´m leaving the answer in case someone else stumbles upon here like I did. Here is (from version 1 from 1.8 and up) how to register a plugin:
ZF follows the naming standard: A_B parses to A/B.php . For the plugin, ZF automatically looks into the "path to library", which means that it looks within the directory of your library (where your Zend library is). So the plugin should be as follows: library/Something/Whatever.php ... That´s one scenario. Then all you have to do in application.ini is add the following:
autoloaderNamespaces[] = "Something_"
resources.frontController.plugins.Whatever = "Something_Whatever"
Translated to your case would be:
autoloaderNamespaces[] = "Common_"
resources.frontController.plugins.Authentication = "Common_Authentication"
And your library structure should be:
library/Common/Authentication.php
hope this helps to anyone stumbling upon here!
--Regarding your post/question
The reason why it´s not "finding" the class it´s because it´s not loading with autoload. One reason might be that you're somehow violating the naming convention (Your Authentication file is not under directory Common_ , or the file name of Authentication class is not Common_Authentication ...). A quick fix would be to put:
//bootstrap
protected function _initAutoloader()
{
require_once 'Common/Authentication.php';
}
with this addes, _initPlugins() will be able to execute without problem. :)
Is there a way to have models for each module? I have 3 modules, one is a "contacts" module.
I created a model for it in modules/contacts/models/Codes.php
Codes Controller
class Contacts_CodesController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
$this->view->messages = $this->_helper->flashMessenger->getMessages();
}
public function indexAction()
{
$codesTable = new Contacts_Model_Codes();
}
Codes Model:
class Contacts_Model_Codes extends Zend_Db_Table
{
protected $_name = 'codes';
}
The error I get:
Fatal error: Class 'Contacts_Model_Codes' not found in /Applications/MAMP/htdocs/zf_site/application/modules/contacts/controllers/CodesController.php on line 26
thanks
I found the problem. I forgot to put a bootstrap file in with my contacts module.
Now it all works and I can have my modules use their own models.
class Contacts_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
I've found the solution, I guess! :)
It's a problem when you add the next Resource in the application.ini file
resources.frontController.defaultModule = "Default"
and also you use some kind of parameters. I think that is a Bug.
The correct way to implement Modules is:
1 - Create your desired modules and the 'Default' Module with zf tool
2 - In apllication.ini tell ZF where the modules are and where the controllers of those modules are, with
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.moduleControllerDirectoryName = "controllers"
Use the known
resources.modules = ""
And set:
resources.frontController.params.prefixDefaultModule = ""
It's important because zf tool set it to "1". Here is the bug. :)
And remember DO NOT PUT WHAT THE DEFAULT MODULE IS!!
3 - Create the bootstrap file for each module and put:
If my module is 'Evacol':
<?php
class Evacol_Bootstrap extends Zend_Application_Module_Bootstrap
{
}
Save it to /modules/Evacol/ obviously
Take note of Evacol_... and ..._Module_Bootstr... THE NAME OF MY MODULE EXTENDING THE CORRECT CLASS.
Don't use the default value of bootstrap file created with zf tool. I did it :)
DON'T MODIFY ANYTHING ELSE. IT IS NOT NECESARY.
And voila! Trust me. It works!
It was Zend Framework 1.10.8
You have to register the 'Contacts_' namespace with the auto loader. You can use Zend_Application_Module_Autoloader for this.
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Contacts_',
'basePath' => dirname(__FILE__) . '/modules/cotacts',
));
This will create the following mappings for your module inside the basePath you provide.
api/ => Api
forms/ => Form
models/ => Model
DbTable/ => Model_DbTable
plugins/ => Plugin
If you are using Zend_Application to boostrap your application and it' modules you should not need this because the docs say that:
When using module bootstraps with Zend_Application, an instance of Zend_Application_Module_Autoloader will be created by default for each discrete module, allowing you to autoload module resources.
add
resources.modules[] =
To your config ini
I'm using version 1.9.
This is part of my bootstrap:
protected function _initAutoload() {
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH.'/modules/default')
);
}
protected function _initAutoloaders()
{
$this->getApplication()->setAutoloaderNamespaces(array('Eric_'));
return $this;
}
protected function _initPlugins()
{
$this->bootstrap('autoloaders');
$this->bootstrap('frontController');
// register the plugin for setting layouts per module
$plugin = new Eric_Plugin_Modularlayout();
$this->frontController->registerPlugin($plugin);
return $modelLoader;
}
The plugin Eric_Plugin_Modularlayout sets the correct layout for each module.
I have 3 modules: default, ez, contacts.
The funny thing is, In a contacts action I can call the models in the ez/models dir. without a problem.