zend translate join multiple file languages - zend-framework

i am trying to add to my language file, the zend form errors language file, which can be found in
ZendFramework-1.11.2.zip\ZendFramework-1.11.2\resources\languages
in order to have localized form validators erorrs. This blog http://www.thomasweidner.com/flatpress/2010/03/25/working-with-multiple-translation-sources/comments/ explains what i try to do, but it fails :( it only translates from my .ini file, the .php is not included
Here is what i did:
this is my application.ini
resources.translate.adapter = "ini"
resources.translate.default.locale = "it_IT"
resources.translate.default.locale = "en_US"
resources.translate.default.file = APPLICATION_PATH "/lang/source-it.ini"
resources.translate.translation.en = APPLICATION_PATH "/lang/source-en.ini"
;resources.translate.data.directory = APPLICATION_PATH "/lang"
resources.translate.data = APPLICATION_PATH "/lang"
and then i have this
<?php
class Gestionale_Application_Resource_Translate extends Zend_Application_Resource_ResourceAbstract
{
public function init ()
{
$options = $this->getOptions();
$adapter = $options['adapter'];
$defaultTranslation = $options['default']['file'];
$defaultLocale = $options['default']['locale'];
$translate = new Zend_Translate($adapter, $defaultTranslation, $defaultLocale);
$translation_addme = new Zend_Translate(
'array',
APPLICATION_PATH."/resources/languages/it/Zend_Validate.php"
'it',
array('scan' => Zend_Translate::LOCALE_DIRECTORY)
);
$translate->addTranslation($translation_addme);
foreach ($options['translation'] as $locale => $translation) {
$translate->addTranslation($translation, $locale);
}
Zend_Registry::set('Zend_Translate', $translate);
return $translate;
}
}
and this is my dir
c:\www\www\gestionale\application>dir /w /aD
Il volume nell'unità C è OS
Numero di serie del volume: 6A5E-FD9B
Directory di c:\www\www\gestionale\application
[.] [..] [.svn] [configs] [controllers]
[forms] [lang] [layouts] [models] [resources]
[views]
0 File 0 byte
11 Directory 447.780.282.368 byte disponibili

Just by looking at your code I cannot tell you much. I suspect that your problems might be because you create your own resource plugin, which is unnessesry for this purpose, somethings are not being bootstrap properly, or there are some conflicts with your application.ini. Any way, I will post my translation setup and maybe it will give you some clues.
application.ini
Nothing here about translation or locale since I setup them in Bootstrap.php.
Bootstrap.php
protected function _initLocale() {
// define locale
$locale = new Zend_Locale('en');
// register it so that it can be used all over the website
Zend_Registry::set('Zend_Locale', $locale);
}
protected function _initTranslate() {
// Get Locale
$locale = Zend_Registry::get('Zend_Locale');
// Set up and load the translations (there are my custom translations for my app)
$translate = new Zend_Translate(
array(
'adapter' => 'array',
'content' => APPLICATION_PATH . '/languages/' . $locale . '.php',
'locale' => $locale)
);
// Set up ZF's translations for validation messages.
$translate_msg = new Zend_Translate(
array(
'adapter' => 'array',
'content' => APPLICATION_PATH .
'/resources/languages/' . $locale . '/Zend_Validate.php',
'locale' => $locale)
);
// Add translation of validation messages
$translate->addTranslation($translate_msg);
Zend_Form::setDefaultTranslator($translate);
// Save it for later
Zend_Registry::set('Zend_Translate', $translate);
}

Related

how to differentiate admin module from main application in bootstrap file in zend framework

Please give me proper solution for my query. I have tried to solve it but not getting any proper solution. Please give me proper solution.
If I remove following line from application.ini file then it is working well for front end application
resources.modules[] =
After remove it, I am unable to get my created module(Admin) in modules folder with proper layout. I have only one module. In module bootstrap file I have defined following functions (project/application/modules/admin/Bootstrap.php)
<?php
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAppAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'admin',
'basePath' => APPLICATION_PATH . '/modules/admin/'
));
return $autoloader;
}
protected function _initPlugins()
{
$bootstrap = $this->getApplication();
if ($bootstrap instanceof Zend_Application) {
$bootstrap = $this;
}
$bootstrap->bootstrap('FrontController');
$front = $bootstrap->getResource('FrontController');
$plugin = new Admin_Plugin_Layout();
// $plugin->setBootstrap($this);
$front->registerPlugin($plugin);
}
protected function _initAuthPlugin()
{
$checkAuth = Zend_Controller_Front::getInstance();
$checkAuth->registerPlugin(new Admin_Plugin_CheckAuth(Zend_Auth::getInstance()));
}
protected function _initDoctype()
{
global $adminModuleCssPath;
global $adminModuleJsPath;
$this->bootstrap( 'view' );
$view = $this->getResource( 'view' );
$view->headTitle('Jyotish - Ek Gyan');
$view->headScript()->appendFile($adminModuleJsPath.'jquery-1.7.2.js');
$view->headScript()->appendFile($adminModuleJsPath.'jquery-ui.js');
$view->headScript()->appendFile($adminModuleJsPath.'tinybox.js');
$view->headScript()->appendFile($adminModuleJsPath.'common.js');
$view->headLink()->appendStylesheet($adminModuleCssPath.'jquery-ui.css');
$view->headLink()->appendStylesheet($adminModuleCssPath.'style.css');
$view->headLink()->appendStylesheet($adminModuleCssPath.'theme.css');
$view->headLink()->appendStylesheet($adminModuleCssPath.'tinybox.css');
$view->doctype( 'XHTML1_STRICT' );
//$view->navigation = $this->buildMenu();
}
protected function _initLayoutPlugin()
{
$layout = Zend_Controller_Front::getInstance();
$layout->registerPlugin(new Admin_Plugin_AdminLayout());
}
protected function _initRouter()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
':module/:controller/:action/*',
array('module' => 'admin')
);
$router->addRoute('default', $route);
$usersRoute = new Zend_Controller_Router_Route_Regex(
':module/:controller/:action/(?:/page/(\d+)/?)?',
array(
'module' => 'admin',
'controller' => 'users',
'action' => 'index',
'page' => 1,
),
array(
'page' => 1,
)
);
$router->addRoute('users-index', $usersRoute);
}
protected function _initActionHelpers()
{
Zend_Controller_Action_HelperBroker::addPath(APPLICATION_PATH . "/modules/admin/views/helpers");
Zend_Controller_Action_HelperBroker::addPrefix('Admin_View_Helper');
}
}
In modules folder I have created following plugin Layout
class Admin_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
if ('admin' != $request->getModuleName()) {
// If not in this module, return early
return;
}
// Change layout
Zend_Layout::getMvcInstance()->setLayout('admin');
}
}
In frontend bootstrap file I have defined following functions(project/application/Bootstrap.php)
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAppAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'default',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
protected function _initLayoutHelper()
{
$this->bootstrap('frontController');
$layout = Zend_Controller_Action_HelperBroker::addHelper(
new Application_View_Helper_LayoutLoader());
}
}
I have created following helper file in (project/application/view/helper/LayoutLoader.php)
<?php
class Application_View_Helper_LayoutLoader extends Zend_Controller_Action_Helper_Abstract
{
public function preDispatch()
{
$bootstrap = $this->getActionController()
->getInvokeArg('bootstrap');
$config = $bootstrap->getOptions();
$module = $this->getRequest()->getModuleName();
if (isset($config[$module]['resources']['layout']['layout'])) {
$layoutScript =
$config[$module]['resources']['layout']['layout'];
$this->getActionController()
->getHelper('layout')
->setLayout($layoutScript);
}
}
}
From last two days I am trying to create separate layout for both but I am unable to getting proper solution. When I run admin module in browser, it is working well but when I run frontend application folder it show exception error with layout of admin.
Please provide me proper solution....
Thanks
The standard way to do a layout switching is with a front-controller plugin. You don't need the LayoutLoader helper with preDispatch hook.
A simple layout-switcher plugin can be implemented as follows.
Place your various layout files in application/layouts/scripts/, named the same as your module: default.phtml, admin.phtml, etc.
In the file application/plugins/Layout.php:
class Application_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
Zend_Layout::getMvcInstance()->setLayout($request->getModuleName());
}
}
Enable the plugin application/configs/application.ini using:
resources.frontController.plugins.layout = "Application_Plugin_Layout"
or by manually registering the plugin in Bootstrap.
Also, make sure your application.ini enables modules and identifies your layout location:
resources.modules[]=
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
You seem to be way over thinking this. I'm going to simplify this down to the basic and you can add back as you see fit.
The bootstrap system in ZF1 is a bad joke. Anything that is present in one of the bootstrap files will available to all modules at run and will be run with every request. So keep it simple and keep it light. I normally put all of the _init methods in the main bootstrap and leave the module bootstraps empty.
//simplify your bootstrap to minimum
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
//your app autoloader is the standard autoloader so is not needed here, it better just work.
/**
* initialize the session
*/
protected function _initsession()
{
Zend_Session::start();
}
/**
* initialize the registry and assign application.ini to config namespace
*/
protected function _initRegistry()
{
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
}
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 default title
$view->headTitle('Jyotish - Ek Gyan');
//set css includes
$view->headlink()->setStylesheet('/bootstrap/css/bootstrap.min.css');
//add javascript files
$view->headScript()->setFile('/bootstrap/js/jquery.min.js');
$view->headScript()->appendFile('/bootstrap/js/bootstrap.min.js');
//add it 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;
}
/**
* Not sure if this is really required as it seems as though your routes are pretty standard.
*/
protected function _initRouter()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
':module/:controller/:action/*',
array('module' => 'admin')
);
$router->addRoute('default', $route);
$usersRoute = new Zend_Controller_Router_Route_Regex(
':module/:controller/:action/(?:/page/(\d+)/?)?',
array(
'module' => 'admin',
'controller' => 'users',
'action' => 'index',
'page' => 1,
),
array(
'page' => 1,
)
);
$router->addRoute('users-index', $usersRoute);
}
protected function _initAuthPlugin()
{
$checkAuth = Zend_Controller_Front::getInstance();
$checkAuth->registerPlugin(new Admin_Plugin_CheckAuth(Zend_Auth::getInstance()));
}
}
The application.ini is a very important part of application configuration:
//truncated for example
[production]
;-------------------------------------------------------------------------------
;//PHP
;-------------------------------------------------------------------------------
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
;-------------------------------------------------------------------------------
;//Paths and Namespaces, paths for application resources and library
;-------------------------------------------------------------------------------
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
autoloaderNamespaces[] = "My_"
;-------------------------------------------------------------------------------
;//Front Controller, default settings for controllers and modules
;-------------------------------------------------------------------------------
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, default paths for resource plugins and action helpers, can also be
; //accomplished in the bootstrap
;-------------------------------------------------------------------------------
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, view settings to be shared with the view object in the bootstrap
;// allows for easier editing.
;-------------------------------------------------------------------------------
resources.view[]=
resources.view.charset = "UTF-8"
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.language = "en"
resources.view.contentType = "text/html; charset=UTF-8"
;-------------------------------------------------------------------------------
;//Database Settings, default database adapter settings
;-------------------------------------------------------------------------------
resources.db.adapter = "pdo_Mysql"
resources.db.params.username = "user"
resources.db.params.password = "xxxx"
resources.db.params.dbname = "dbname"
resources.db.params.charset = "utf8"
resources.db.isDefaultTableAdapter = true
resources.db.params.profiler = true
;-------------------------------------------------------------------------------
;//Layouts, default path to layout files and default layout name
;-------------------------------------------------------------------------------
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = layout
Now the simplest way to accomplish layout switching is to switch the layout on a per controller basis, use the default layout except where you explicitly change it:
//some controller that needs the admin layout
//note: preDispatch() and routeShutdown() are essentially the same point in the loop
public function preDispatch() {
//switch to new layout
$this->_helper->layout->setLayout('admin');
//you can also manipulate the css and js similarly to the bootstrap
$this->view->headScript()->appendFile(
'/javascript/mediaelement/build/mediaelement-and-player.min.js');
$this->view->inlineScript()->setScript(
"$('audio').mediaelementplayer();");
}
I find that if I just need one or two different layouts this works nicely and is simple. Not really a DRY solution but can really help develop the parameters for a plugin if a DRY solution is desired.
I hope this provides some help, the big thing to remember is that the module bootstraps are basically just extentions of the main bootstrap and provide no seperation of functionality.
P.S. I would have provided a plugin demo but I really suck at plugins, Sorry.

module in zend framework not loading index controller

Following Pro Zend Framework techniques, I have created a module named 'Contact' in modules directory. Here's the directory structure.
|_application
|_Configs
|_application.ini
|_controllers
|_modules
|_Contact
|_Controllers
|_IndexController
|_models
|_views
Bootstrap.php
Bootstrap.php
The Bootstrap file in the application directory has an _initAutoload() function as shown below:
protected function _initAutoLoad(){
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('CMS_');
$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_'))));
return $autoLoader;
}
The Bootstrap file in the modules is:
class Contact_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoLoad(){
$autoloader=new Zend_Application_Module_Autoloader(array('namespace'=>'Contact_',
'basePath'=>dirname(__FILE__),));
return $autoLoader;
}
}
The application.ini file in the config folder has the following lines for setting up the module 'contact':
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
contact.resources.frontController.defaultControllerName = "index"
I have set up an application error controller which has a getmessage() function to display the error. When I try to load, http://localhost/zf_cms/public/contact, it gives an error:
getMessage() : Invalid controller specified (index)
The name of the index controller in modules->contact->controller is Contact_IndexController. I also created a view for index controller.
Please help me find the bug and let me know if I missed some info.
Thank you.
rename module and controller directory to lowercase . And it seems from your post that you are not adding .php extension to your IndexController do that aswell .

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_Translate scan translation files

I've trying to use Zend_Translate from Zend Framework
I am using "POEdit" to generate "gettext" translation files.
My files are under /www/mysite.com/webapps/includes/locale (this path is in my include path).
I have:
pictures.en.mo
pictures.en.po
(I plan on having pictures.es.mo soon)
It all works fine if I manually do addTranslation() for each file. However I want to use the automatic file scanning method.
I tried both of those:
<?php
/*Localization*/
require_once 'Zend/Translate.php';
require_once 'Zend/Locale.php';
define('LOCALE','/www/mysite.com/webapps/includes/locale');
if(!empty($_GET['locale'])){
$locale = new Zend_Locale($_GET['locale']);
}
else{
$locale = new Zend_Locale();
}
$translate = new Zend_Translate('gettext', LOCALE, null, array('scan' => Zend_Translate::LOCALE_FILENAME));
if ( $translate->isAvailable( $locale->getLanguage() ) ){
$translate->setLocale($locale);
}
else{
$translate->setLocale('en');
}
And this:
<?php
/*Localization*/
require_once 'Zend/Translate.php';
require_once 'Zend/Locale.php';
define('LOCALE','/www/mysite.com/webapps/includes/locale');
if(!empty($_GET['locale'])){
$locale = new Zend_Locale($_GET['locale']);
}
else{
$locale = new Zend_Locale();
}
$translate = new Zend_Translate('gettext', LOCALE);
if ( $translate->isAvailable( $locale->getLanguage() ) ){
$translate->setLocale($locale);
}
else{
$translate->setLocale('en');
}
In both cases, I get a Notice: No translation for the language 'en' available. in /www/mysite.com/webapps/includes/Zend/Translate/Adapter.php on line 411
It also worked if I tried to do directory scanning.
i think there is just one little "bug".
$translate = new Zend_Translate(
'gettext',
LOCALE,
null,
array('scan' => Zend_Translate::LOCALE_DIRECTORY) // <--
);
If you use LOCALE_FILENAME, is ZF searching inside this FILE for your Translation.

Module configuration and layout configuration in zend framework

I got some codes from other articles for configuring module and layout in zend framework. I tried with in my local. i didn't get different layout for default and admin module. Here is my code for configuring module and layout for zend framework.
configs/application.ini
[production]
# Debug output
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
# Include path
includePaths.library = APPLICATION_PATH "/../library"
# Bootstrap
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
admin.bootstrap.path = APPLICATION_PATH "/modules/admin/Bootstrap.php"
admin.bootstrap.class = "admin_Bootstrap"
# Front Controller
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.env = APPLICATION_ENV
# Session
resources.session.name = "ZendSession"
resources.session.save_path = APPLICATION_PATH "/../data/session"
resources.session.remember_me_seconds = 86400
# Layout
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts"
admin.resources.layout.layout = "admin"
admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts"
# Views
resources.view.encoding = "UTF-8"
resources.view.basePath = APPLICATION_PATH "/views/"
resources.view[] =
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =
resources.view[] =
admin.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
application/Bootstrap.php
<?php
/**
* Ensure all communications are managed by sessions.
*/
require_once ('Zend/Session.php');
Zend_Session::start();
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initDoctype() {
$this->bootstrap( 'view' );
$view = $this->getResource( 'view' );
$view->navigation = array();
$view->subnavigation = array();
$view->headTitle( 'Module One' );
$view->headLink()->appendStylesheet('/css/clear.css');
$view->headLink()->appendStylesheet('/css/main.css');
$view->headScript()->appendFile('/js/jquery.js');
$view->doctype( 'XHTML1_STRICT' );
//$view->navigation = $this->buildMenu();
}
/*protected function _initAppAutoLoad()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'default',
'basePath' => APPLICATION_PATH
));
return $autoloader;
}*/
protected function _initLayoutHelper()
{
$this->bootstrap('frontController');
$layout = Zend_Controller_Action_HelperBroker::addHelper(
new ModuleLayoutLoader());
}
public function _initControllers()
{
$front = Zend_Controller_Front::getInstance();
$front->addModuleDirectory(APPLICATION_PATH . '/modules/admin/', 'admin');
}
protected function _initAutoLoadModuleAdmin() {
$autoloader = new Zend_Application_module_Autoloader(array(
'namespace' => 'Admin',
'basePath' => APPLICATION_PATH . '/modules/admin'
));
return $autoloader;
}
protected function _initModuleutoload() {
$autoloader = new Zend_Application_Module_Autoloader ( array ('namespace' => '', 'basePath' => APPLICATION_PATH ) );
return $autoloader;
}
}
class ModuleLayoutLoader extends Zend_Controller_Action_Helper_Abstract
// looks up layout by module in application.ini
{
public function preDispatch()
{
$bootstrap = $this->getActionController()
->getInvokeArg('bootstrap');
$config = $bootstrap->getOptions();
echo $module = $this->getRequest()->getModuleName();
/*echo "Configs : <pre>";
print_r($config[$module]);*/
if (isset($config[$module]['resources']['layout']['layout'])) {
$layoutScript = $config[$module]['resources']['layout']['layout'];
$this->getActionController()
->getHelper('layout')
->setLayout($layoutScript);
}
}
}
application/modules/admin/Bootstrap.php
<?php
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
/*protected function _initAppAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'admin',
'basePath' => APPLICATION_PATH . '/modules/admin/'
));
return $autoloader;
}*/
protected function _initDoctype() {
$this->bootstrap( 'view' );
$view = $this->getResource( 'view' );
$view->navigation = array();
$view->subnavigation = array();
$view->headTitle( 'Module One' );
$view->headLink()->appendStylesheet('/css/clear.css');
$view->headLink()->appendStylesheet('/css/main.css');
$view->headScript()->appendFile('/js/jquery.js');
$view->doctype( 'XHTML1_STRICT' );
//$view->navigation = $this->buildMenu();
}
}
Please go through it and let me know any knows how do configure module and layout in right way..
Thanks and regards,
Prasanth P
I use plugin approach with this code I have written:
in main Bootstrap:
protected function _initPlugins()
{
// Access plugin
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new MyApp_Plugin_Module());
}
In plugin directory:
class MyApp_Plugin_Module extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$module = $request->getModuleName();
$layout = Zend_Layout::getMvcInstance();
// check module and automatically set layout
$layoutsDir = $layout->getLayoutPath();
// check if module layout exists else use default
if(file_exists($layoutsDir . DIRECTORY_SEPARATOR . $module . ".phtml")) {
$layout->setLayout($module);
} else {
$layout->setLayout("default");
}
}
Hope it helps.
From your code:
# Layout
resources.layout.layout = "layout"
resources.layout.layoutPath = APPLICATION_PATH "/layouts"
admin.resources.layout.layout = "admin"
admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts"
you are using your_app/modules/admin/layouts/admin.phtml as admin module layout, and I guess it replaced your_app/layouts/layout.phtml. Check a way to switch between modules and try something site.ressources.layout instead of resources.layout.layout. i am a newbie to zend. check out how to setting up you bootstrap at http://www.survivethedeepend.com/
the same problem and solution has been stressed here: http://blog.astrumfutura.com/archives/415-Self-Contained-Reusable-Zend-Framework-Modules-With-Standardised-Configurators.html
In my application I configured this way. It worked perfectly.
protected function _initLayout(){
$layout = explode('/', $_SERVER['REQUEST_URI']);
if(in_array('admin', $layout)){
$layout_dir = 'admin';
}else if(in_array('default', $layout)){
$layout_dir = 'default';
}else{
$layout_dir = 'default';
}
$options = array(
'layout' => 'layout',
'layoutPath' => APPLICATION_PATH."/modules/".$layout_dir."/views/layouts"
);
Zend_Layout::startMvc($options);
}
You need to use a Controller Plugin to achieve that, because the layout is set based on the request entry, and on the bootstrap the application hasn't been dispatched, so you need to use a controller plugin to work on the preDispatch to switch layouts.
I think the easiest way is check the URI_String. Please see below:
I have a module named as "admin".
Under layout folder I have 2 directories. "site" and "admin"
\application\layout\site\layout.phtml and \application\layout\admin\layout.phtml
Add this block of code on Bootstrap.php
It just change the layout directory path.
protected function _initLayout(){
$layout = explode('/', $_SERVER['REQUEST_URI']);
if(in_array('admin', $layout)){
$layout_dir = 'admin';
}else{
$layout_dir = 'site';
}
$options = array(
'layout' => 'layout',
'layoutPath' => APPLICATION_PATH . "/layouts/scripts/".$layout_dir,
);
Zend_Layout::startMvc($options);
}
Your questions answered my question, that's right, I was trying to find out why it did not work in my bootstrap modules, seen in its configuration file that you need to add the line
administrador.resources.view [] =
Valew partner!
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initAutoload() {
$autoloader = Zend_Loader_Autoloader::getInstance();
$moduleLoader = new Zend_Application_Module_Autoloader(
array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules'
)
);
return $moduleLoader;
}
protected function _initViewhelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
}
protected function _initNavigation()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
$navigation = new Zend_Navigation($config);
$view->navigation($navigation);
}
}
Layout and module in not enabled on a newly zend project (in ZF version 1). It needs to be enabled and you need to make it work.
Layout works for the common header and footer for the working zend project, on the other hand module can be used for the different kind of access i.e module for user, module for admin, module for visitor and so on.
For a quick reference you can find a complete explanation with a complete project to get the basic idea from here, on my site. . http://www.getallthing.com/how-to-use-layout-and-module-in-zend-framework/
Good luck and cheers!
$options = array(
'layout' => 'layout',
'layoutPath' => APPLICATION_PATH."/modules/".$layout_dir."/views/layouts"
);
Zend_Layout::startMvc($options);
Tried a few other solutions from SOF and this one worked great. Just needed to point the layoutPath to the folder of the actual layouts