I have made module Admin. In this module, in controller I have called form
class Admin_AdminController extends Zend_Controller_Action
{
public function indexAction()
{
//$form = new Application_Form_Login();
$form = new Admin_Form_Admin();
$this->view->form = $form;
}
}
But In controller its giving error -> Class 'Admin_Form_Admin' not found in application\modules\Admin\controllers\AdminController.php
My forms is in application\modules\Admin\forms\Admin.php.Below is my form code
class Admin_Form_Admin extends Zend_Form
{
public function init()
{
this->setMethod('post');
/* Form Elements & Other Definitions Here ... */
$user = $this ->CreateElement('text','username');
$password = $this->createElement('text','password');
$login = $this->createElement('submit','button');
$this->addElements(array($user,
$password,
$login
));
}
}
Three things are required here...
You need to bootstrap the modules resource
; application.ini
resources.modules[] =
You need to set the modules directory in your front controller
; application.ini
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
You need a bootstrap class in your admin module
<?php
// application/modules/Admin/Bootstrap.php
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {}
With these three things in place, your code should work as-is.
Related
When i go to controllers index URL preDispatch is called before entering indexAction method of that controller but postDispatch is not called after it. Why it is not called and should i force it somehow to call postDispatch?
I have two plugin with postDispach methods whitch are not called.
If i got to URL e.g admin/listperms postDispatch is called and everything works.
AdminController.php
class AdminController extends Application_Controllers_Base {
public function indexAction() {
$this->view->headTitle = $this->alias('TITLE_ADMIN_PANEL');
}
public function listpermsAction() {
$this->view->headTitle = $this->alias('TITLE_PERMISSION_LIST');
$permService = new Application_Services_AdminPermission();
$perms = $permService->getPermissionList();
if(!$perms) {
// TODO:
} else {
$this->view->permList = $perms;
}
}
}
Base.php
class Application_Controllers_Base extends Zend_Controller_Action {
}
DbLogPlugin.php
class Application_Plugin_DbLogPlugin extends Zend_Controller_Plugin_Abstract {
public function postDispatch(Zend_Controller_Request_Abstract $request) {
$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
$view->developerLog = $this->getQuerysLogs();
}
}
bootstrap.php
protected function _initAutoload(){
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Application_', //'
'basePath' => APPLICATION_PATH
));
$autoloader->addResourceType('dao', 'dao', 'Dao');
$autoloader->addResourceType('services', 'services', 'Services');
$autoloader->addResourceType('plugins', 'plugins', 'Plugins');
$autoloader->addResourceType('models', 'models', 'Models');
$autoloader->addResourceType('controllers', 'controllers', 'Controllers');
require_once('controllers/FrontController.php');
$front = Application_Controllers_FrontController::getInstance();
$front->setControllerDirectory(APPLICATION_PATH.'/controllers')
->setRouter(new Zend_Controller_Router_Rewrite())
->registerPlugin(new Application_Plugin_DbLogPlugin())
->registerPlugin(new Application_Plugin_AuthPlugin())
->registerPlugin(new Application_Plugin_ViewPlugin());
return $autoloader;
}
I am trying to make a stand alone Zend Form with a custom form element. I need use custom view helper to create this element. How do I register a custom view helper path without an application.ini file?
set_include_path(
implode(PATH_SEPARATOR, array(
get_include_path(),
PATH_TO_ZF_LIBRARY
)));
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace( 'My' );
$form = new Zend_Form;
... create and add zend form elements here
//display form
echo $form->render(new Zend_View());
Also, will the custom Zend_Form_Element know to look for the custom helper in the new path? According to the docs all I have to do is create public $helper var with the class name of the view helper. But I can't figure out if that will work with a custom view helper.
class My_Form_Element_Ssn extends Zend_Form_Element_Xhtml
{
public $helper = 'ssnElement';
public function setValue()
{
}
public function getValue()
{
return '12345';
}
}
class My_View_Helper_SsnElement
extends Zend_View_Helper_FormElement
{
public function ssnElement( $name, $value = null, $attribs = null )
{
return 'SSN';
}
}
I thank you in advance for your assistance.
Try:
$view = new Zend_View();
$view->addHelperPath('/path/to/My/View/Helper', 'My_View_Helper');
echo $form->render($view);
Where is the best place to create a class that contain all the application constant variables ?
is it :
- Bootstrap
- In the Application Common library
for example :
1- When i retrieve an image name from a database if this record doesnt have an image , i want to put a default value somewhere so i can use it in my models
** a constants that i use in all my application so if i change it , i dont want to go back to all in my code and change it everywhere
application.ini is the best place for example define some constant there
constants.PUBLIC_PATH = APPLICATION_PATH "/../public/"
constants.THEME = blue
Then in your bootstrap do
protected function setConstants($constants)
{
foreach($constants as $name => $value)
{
if(!defined($name))
define($name, $value);
}
}
ZF take 'constants' from config and call setConstants method in your bootstrap passing all lines prefixed by constants hence its an array .
I try not use constants and prefer class constants over global constants. However, when constants are unavoidable, for whatever reason, I go with .ini config file and Bootstrap/library/model class constants.
An example of .ini config constants
(assumes default zf project structure)
application/configs/application.ini
constants.MESSAGE_1 = "Message 1"
constants.MESSAGE_2 = "Message 2"
Bootstap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initConstants()
{
$options = $this->getOption('constants');
if (is_array($options)) {
foreach($options as $key => $value) {
if(!defined($key)) {
define($key, $value);
}
}
}
}
// ...
}
Example usage in controller:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->message1 = MESSAGE_1;
$this->view->message2 = MESSAGE_2;
}
}
I would extend on the above to allow configuration of how your constants are defined. For example you may want all constants UPPERCASED or not, and allow or disallow already defined constants, so:
application/configs/application.ini
constants.forceUppercase = 1
constants.allowAlreadyDefined = 1
constants.set.message_1 = "Message 1"
constants.set.message_2 = "Message 2"
Bootstrap:
protected function _initConstants()
{
$options = $this->getOption('constants');
if (isset($options['set']) && is_array($options['set'])) {
if (isset($options['forceUppercase'])
&& (bool) $options['forceUppercase']) {
$options['set'] = array_change_key_case($options['set'], CASE_UPPER);
}
$allowAlreadyDefined = false;
if (isset($options['allowAlreadyDefined'])
&& (bool) $options['allowAlreadyDefined']) {
$allowAlreadyDefined = true;
}
foreach($options['set'] as $key => $value) {
if (!defined($key)) {
define($key, $value);
} elseif (!$allowAlreadyDefined) {
throw new InvalidArgumentException(sprintf(
"'%s' already defined!", $key));
}
}
}
}
Bootstrap class constants
Could be your own library, or model class, etc., it depends.
In the bootstrap:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
const MESSAGE_CONSTANT = "Hello World";
// ...
}
Example usage in controller:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->message = Bootstrap::MESSAGE_CONSTANT;
}
}
My application has the structure :
myapp
- applcation
- modules
- default
- controllers
- models
- Acl.php
- views
- Bootstrap.php
- admin
- controllers
- models
- ResourceMapper.php
- RoleMapper.php
- views
- Bootstrap.php
- helpers
- layout
- library
- index.php
I want to call classes in the Models folder in admin-module from Acl.php (in the default module). Here my code :
class Model_Acl extends Zend_Acl
{
public $_roleModelMapper;
public $_resourceModelMapper;
function init(){
$this->_roleModelMapper = new Admin_Model_RoleMapper();
$this->_resourceModelMapper = new Admin_Model_ResourceMapper();
}
public function initRole(){
$roles = $this->_roleModelMapper->fetchAll();
foreach ($roles as $role){
$this->addRole($role->getRole());
}
}
public function initResource(){
$resources = $this->_resourceModelMapper->fetchAll();
foreach ($resources as $resource){
if ($resource->getParent()==''){
$this->add(new Zend_Acl_Resource($resource->getResource()));
}else{
$this->add(new Zend_Acl_Resource($resource->getResource()),$resource->getParent());
}
}
}
public function __construct()
{
self::init();
//init role
self::initRole();
//init resource
self::initResource();
//other code here
}
}
But I had error like this:
Fatal error: Class 'Admin_Model_RoleMapper' not found in C:\xampp\htdocs\test2\application\modules\default\models\Acl.php on line ...
Here my application\Bootstrap.php :
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
private $_acl = null;
private $_auth = null;
public function _initAutoload()
{
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH . "/modules/default",
));
//$this->_acl = new Model_Acl();
$this->_acl = new Model_Acl();
$this->_auth = Zend_Auth::getInstance();
if($this->_auth->hasIdentity()){
Zend_Registry::set('role',$this->_auth->getStorage()->read()->role);
}else{
Zend_Registry::set('role','GUEST');
}
$frontController = Zend_Controller_Front::getInstance();
$frontController->registerPlugin(new Plugin_AccessCheck($this->_acl));
return $modelLoader;
}
}
?>
My application.ini does have :
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = ""
So what's wrong with my code? Please help me.
Regards.
It may be useful to see how your admin module bootstrap class looks like (application/modules/admin/Bootstrap.php).
Is it extending Zend_Application_Module_Bootstrap or Zend_Application_Bootstrap_Bootstrap ?
It should extends Zend_Application_Module_Bootstrap otherwise the module resource loader is not initialized with the proper namespace.
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {
}
I am new with zend and I have a problem changing my layout in the bootstrap. I want to change my layout when the user is logged in.
My function to change the layout in the bootstrap is like this:
protected function _initAuthState()
{
$layout = new Zend_Layout;
$layout->setLayoutPath('/layouts/scripts');
if (Zend_Auth::getInstance()->hasIdentity()):
// Logged in.
$layout->setLayout(layout2);
else:
// Not Logged in.
$layout->setLayout(‘layout’);
endif;
}
This code doesn't work, the layout is always the same ... help!
You are modifying a new layout instance, not the instance that is being used by the system.
I assume you are specifying your layout params in application.ini. So you need:
$this->bootstrap('layout');
$layout = $this->getResource('layout');
Then perform your check/modification on this layout instance.
BTW, changing layout is often done using a front-controller plugin. Still runs early enough to do the job, but is often more configurable and re-usable. See here and here for two examples.
I found the answer!!
this is my final result, and is working!!
Bootstrap.php:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initLoader(){
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => '../application/',
'namespace' => 'My',
));
$resourceLoader->addResourceTypes(array(
'plugin' => array(
'path' => 'plugins/',
'namespace' => 'Plugin',
)
));
}
public function _initPlugins()
{
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new My_Plugin_Layout());
}
}
application/plugins/Layout.php:
<?php
class My_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
public function preDispatch()
{
$user = Zend_Auth::getInstance();
$role = $user->getIdentity()->role;
$layout = Zend_Layout::getMvcInstance();
switch ($role) {
case 'admin':
$layout->setLayout('layout2');
break;
case 'normal':
$layout->setLayout('layout');
break;
default:
$layout->setLayout('layout');
break;
}
}
}
?>