Couldnt call A Model Class in Another Module From Default Module? - zend-framework

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 {
}

Related

Why Zend plugin postDispatch method is not called after indexAction

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;
}

Stand alone Zend Form with custom elements

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);

can't access form made in admin module

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.

can't access helper file

My helper file Acl.php is in library/Helper and I have included it in bootstrap file as below:-
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initPlugins()
{
$helper= new Helper_Acl();
// $helper->setRoles();
// $helper->setResources();
// $helper->setPrivilages();
// $helper->setAcl();
}
}
but its giving error, Saying -> Fatal error: Class 'Helper_Acl' not found in Bootstrap.php.
Below is my helper file
class Helper_Acl
{
public $acl;
public function __construct()
{
$this->acl = new Zend_Acl();
}
}
in the bootstrap.php , try this , provided your class is in a Helper folder in the library :
protected function _initHelpers() {
Zend_Controller_Action_HelperBroker::addPrefix("Helper_");
}
if it doesnt work tell me , there are other methods.
You need to add Helper_ to your autoloadernamespaces. Typically, this is done in application/configs/application.ini:
autoloadernamespaces[] = "Helper_"

Zend: ACL logic in View helpers

Background information:
I'm in my admin module, and I created a view helper in modules/admin/views/helpers/AdminPanel.php. I have a layout plugin that forces my view to use the layout in admin/views/layouts/default.phtml.
I'm trying to access my ACL object to determine whether or not the user has resources in the context of a view helper, and then determine whether to return the admin panel html by parsing a configs/admin-nav.xml or not return anything at all.
I'm calling it in my admin layout like so:
<?php echo $this->AdminPanel(); ?>
And the class code which is blank, which I need to access the acl object in:
class My_View_Helper_AdminPanel extends Zend_View_Helper_Abstract {
public function AdminPanel() {}
}
I tried this:
$acl = Zend_Controller_Action_HelperBroker::getStaticHelper('acl');
But this probably isn't what I'm looking for as it forces the default module's views/layouts/default.phtml to load and errors occur.
Here's my global bootstrap file:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
private $_acl = null;
private $_auth = null;
protected function _initDoctype() {
$this->bootstrap('view');
$view = $this->getResource('view');
$view->setEncoding('UTF-8');
$view->doctype('HTML4_STRICT');
}
protected function _initAutoload() {
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('KG_');
$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;
}
protected function _initAclAuth() {
$this->_acl = new Model_Acl;
$this->_auth = Zend_Auth::getInstance();
}
protected function _initNav() {
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$config = new Zend_Config_Xml( APPLICATION_PATH . '/configs/nav.xml', 'mainNav');
$navigation = new Zend_Navigation( $config );
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin( new KG_Controller_Plugin_Acl( $this->_acl, $this->_auth ) );
$role = $this->_auth->getStorage()->read()->role;
if ( !$role ) {
$role = 'guest';
}
$view->navigation( $navigation )->setAcl( $this->_acl)->setRole( $role );
}
protected function _initEncoding() {
$fc = Zend_Controller_Front::getInstance();
$response = new Zend_Controller_Response_Http;
$response->setHeader('Content-Type','text/html;charset=utf-8', true);
$fc->setResponse($response);
}
}
Your KG_Controller_Plugin_Acl plugin should be taking care of the access logic, not your view. If the user doesn't have access to the resource, you should error out or redirect the user to somewhere else.
The layout should be set within the controller. Preferably in the init() method with:
$this->_helper->layout->setLayout();
It looks like you followed the same or a similar tutorial as I did for Zend_Acl. I'm posting my plugin for reference, which includes the logic to handle access control from within the plugin:
class App_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
protected $_auth = null;
protected $_acl = null;
public function __construct(Zend_Auth $auth, Zend_Acl $acl)
{
$this->_auth = $auth;
$this->_acl = $acl;
}
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
if ($this->_auth->hasIdentity()) {
$identity = $this->_auth->getIdentity();
$role = $identity->acl_role;
} else {
$role = 'guest';
}
// Mapping to determine which Resource the current
// request refers to (really simple for this example!)
$resource = $request->controller;
$privilege = $request->action;
if (!$this->_acl->has($resource)) {
$resource = null;
}
// ACL Access Check
if (!$this->_acl->isAllowed($role, $resource, $privilege)) {
if ($this->_auth->hasIdentity()) {
// authenticated, denied access, forward to /error/permissions
$request->setModuleName('default');
$request->setControllerName('error');
$request->setActionName('permissions');
} else {
// not authenticated, forward to login form
$request->setModuleName('default');
$request->setControllerName('auth');
$request->setActionName('login');
}
}
}
}