Jump to content

Zend framework Class 'Model_MenuAcl' not found


colap

Recommended Posts

Error:

Fatal error: Class 'Model_MenuAcl' not found in C:\Program Files\Zend\Apache2\htdocs\website\application\layouts\scripts\layout.phtml on line 16

 

MenuAcl.php is in Models/

<?php
class Model_MenuAcl extends Zend_Acl{
public function __construct(){
	$this->add(new Zend_Acl_Resource('index'));

	$this->add(new Zend_Acl_Resource('add'));
	$this->add(new Zend_Acl_Resource('edit'));
	$this->add(new Zend_Acl_Resource('delete'));

	$this->addRole('user1','index');
	$this->allow('user1',array('add','edit','delete'));

}
}

layout.phtml

<head>
<?php echo $this->headScript()->appendFile($this->baseUrl() . '/js/jwplayer.js'); ?>
<?php echo $this->headScript()->appendFile($this->baseUrl() . '/js/swfobject.js'); ?>
<?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/topmenu.css'); ?>
</head>

<?php
$flXml=new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml','nav');
$container = new Zend_Navigation($flXml);
Zend_Registry::set('Zend_Navigation', $container);
?>

<div id="nav-menu">
<?php

$_acl=new Model_MenuAcl;
$_auth=Zend_Auth::getInstance();

echo $this->navigation()->menu()->setAcl($_acl)->setRole($_auth->getStorage()->read()->role);
echo $this->loggedInAs();
echo '<br><br>';
echo 'You are in: ' . $this->navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render();
?></div>
<div class="mainbody"><?php echo $this->layout()->content; ?></div>
<br />

 

In navigation.xml file i have these:

				<add>
					<label>Add</label>
					<controller>index</controller>
					<action>ac</action>
					<resource>add</resource>
				</add>
				<edit>
					<label>Edit</label>
					<controller>index</controller>
					<action>ac</action>
					<resource>add</resource>								
				</edit>
				<delete>
					<label>Delete</label>
					<controller>index</controller>
					<action>ac</action>
					<resource>add</resource>							
				</delete>			

Link to comment
Share on other sites

in your bootstrap try adding this:

protected function _initAutoload ()
     {
       $resourceLoader = new Zend_Loader_Autoloader_Resource();
       $resourceLoader->addResourceType('model', 'models/', 'Model_');
     }

 

Explain this line please.

$resourceLoader->addResourceType('model', 'models/', 'Model_');

Link to comment
Share on other sites

Error:

Warning: Missing argument 1 for Zend_Loader_Autoloader_Resource::__construct(), called in C:\Program Files\Zend\Apache2\htdocs\site\application\Bootstrap.php on line 6 and defined in C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Loader\Autoloader\Resource.php on line 68

 

Notice: Undefined variable: options in C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Loader\Autoloader\Resource.php on line 70

 

Notice: Undefined variable: options in C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Loader\Autoloader\Resource.php on line 73

 

Fatal error: Uncaught exception 'Zend_Loader_Exception' with message 'Options must be passed to resource loader constructor' in C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Loader\Autoloader\Resource.php:75 Stack trace: #0 C:\Program Files\Zend\Apache2\htdocs\site\application\Bootstrap.php(6): Zend_Loader_Autoloader_Resource->__construct() #1 C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(666): Bootstrap->_initAutoload() #2 C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(619): Zend_Application_Bootstrap_BootstrapAbstract->_executeResource('autoload') #3 C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(583): Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap(NULL) #4 C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Application.php(355): Zend_Application_Bootstrap_BootstrapAbstract->bootstrap(NULL) in C:\Program Files\Zend\ZendServer\share\ZendFramework\library\Zend\Loader\Autoloader\Resource.php on line 75

 

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload(){		
	$resourceLoader = new Zend_Loader_Autoloader_Resource();		
        $resourceLoader->addResourceType('model', 'models/', 'Model_');        
}

}

Link to comment
Share on other sites

Apologies, I missed a part of code out :(.

Let me run you through an example of one of my autoloaders. Basically the

Zend_Loader_Autoloader_Resource()

allows you to add and manage namespaces with the with the zend autoloader. Please see the correct example below:

        $resourceLoader = new Zend_Loader_Autoloader_Resource(array('basePath' => APPLICATION_PATH, 'namespace' => ''));
        $resourceLoader->addResourceType('form', 'forms/','Form_');
        $resourceLoader->addResourceType('plugins', 'library/plugins/','Plugins_');
        $resourceLoader->addResourceType('model','models/', 'Model_');

 

Firstly you need to set the base path for the loader, in this case it is set to the application path, the addresourceType then takes in the arguments

 $typeOfresource,$filePath,$namespace

.

So for the model you are instructing the loader to look for classes within the models/ folder with the namespace prefix of Model_.

I hope this helps.

 

Link to comment
Share on other sites

I'm getting this error,

Fatal error: Call to a member function addRole() on a non-object in C:\Program Files\Zend\Apache2\htdocs\site\application\models\MenuAcl.php on line 9

 

<?php
class Model_MenuAcl extends Zend_Acl{
public $_acl=null;
public function __construct(){		

	$onerole=new Zend_Acl_Role('user1');
	$tworole=new Zend_Acl_Role('user2');

	$this->_acl->addRole($onerole);
	$this->_acl->addRole($tworole);     

	$res1=new Zend_Acl_Resource('add');
	$res2=new Zend_Acl_Resource('edit');
	$res3=new Zend_Acl_Resource('delete');

	$this->_acl->addResource($res1);
	$this->_acl->addResource($res2);
	$this->_acl->addResource($res3);

	$this->_acl->allow($onerole,array($res1,$res2));
	$this->_acl->deny($onerole,$res3);		

}
}

Link to comment
Share on other sites

Yes, now it works.

<?php
class Model_MenuAcl extends Zend_Acl{
public $_acl=null;
public function __construct(){		

	$onerole=new Zend_Acl_Role('user1');
	$tworole=new Zend_Acl_Role('user2');

	$this->addRole($onerole);
	$this->addRole($tworole);     

	$res1=new Zend_Acl_Resource('add');
	$res2=new Zend_Acl_Resource('edit');
	$res3=new Zend_Acl_Resource('delete');

	$this->addResource($res1);
	$this->addResource($res2);
	$this->addResource($res3);

	$this->allow($onerole,array($res1,$res2));
	$this->deny($onerole,$res3);		

}
}

Would you please explain this line

$resourceLoader = new Zend_Loader_Autoloader_Resource(array('basePath' => APPLICATION_PATH, 'namespace' => ''));

And if i want to put that MenuAcl.php file somewhere else then what would i have to configure/change?

Link to comment
Share on other sites

this line tells the autoloader where the starting directory is situated , in this case the

APPLICATION_PATH

. so for this it starts at the application folder:

application/

                models/

                            MenuAcl.php

 

for this example the autoloader would keep parsing through the model folders sub directories looking for any classes beginning with the Model_ namespace. So if you were to move this model into another folder i.e:

application/

                models/

                            Auth/

                                    MenuAcl.php

 

then you would have to amend the classes namespace to match :

class Model_Auth_MenuAcl extends Zend_Acl
{

}

 

have a look at the zend documentation :

http://framework.zend.com/manual/en/zend.loader.autoloader.html

 

Link to comment
Share on other sites

this line tells the autoloader where the starting directory is situated , in this case the

APPLICATION_PATH

. so for this it starts at the application folder:

application/

                models/

                            MenuAcl.php

 

for this example the autoloader would keep parsing through the model folders sub directories looking for any classes beginning with the Model_ namespace. So if you were to move this model into another folder i.e:

application/

                models/

                            Auth/

                                    MenuAcl.php

 

then you would have to amend the classes namespace to match :

class Model_Auth_MenuAcl extends Zend_Acl
{

}

 

have a look at the zend documentation :

http://framework.zend.com/manual/en/zend.loader.autoloader.html

What to do if i put the MenuAcl.php file in layouts/script folder or in public folder?

Link to comment
Share on other sites

You should never really put anything besides the index page and javascript, css etc into the public folder. you are in effect making the script accesible to the public domain. One of the main fundamentals of using a framework like Zend is that you application code is kept securely out of the public reach and all required code is passed to the view(index) through the controller.

 

For the type of classes you are referring to (Authentication , access controll etc) you should think about moving the class into your default library folder. The library folder path is already included in the default zend framework structure. So below is an example of creating a plugins folder to house it.

 

application/

docs/

library/

          Plugins/

                      Auth/

                              MenuAcl.php

 

so all you would need to add in your bootstrap would be:

        $resourceLoader = new Zend_Loader_Autoloader_Resource(array('basePath' => APPLICATION_PATH, 'namespace' => ''));
        $resourceLoader->addResourceType('plugins', 'library/plugins/','Plugins_');

and chage your class name to

class Plugins_Auth_Acl extends  Zend_Acl
{
// code here
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.