The things I did is
zf create project demo1 in command prompt
add the lines to application.ini
appnamespace = "Application"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
add a layout with header and footer using partial() (They are perfectly worked)
create Data.php in models directory and add this simple class
<?php class Application_Model_Data{ }//Application for appnamespace
then I tried to load this class(by creating instance)from index controller index action
$data = new Application_Model_Data();
but when I test it even in this level it gives an error
Fatal error: Class 'Application_Model_Data' not found in C:\Zend\...\IndexController.php
Question
Do I want to add a autoloader to
load models in the application( I'm not used modules)
if not what was I missed to add
please help I'm stuck in the beginning,Thank you
this should work!!
add this function to bootstrap:
protected function _initResourceAutoloader()
{
$autoloader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => 'Application',
));
$autoloader->addResourceType( 'model', 'models', 'Model');
return $autoloader;
}
You need to setup a resource Autoloader in your Bootstrap, something like this:
protected function _initResourceAutoloader()
{
$autoloader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => 'path/to/application/directory',
'namespace' => 'Application_',
));
return $autoloader;
}
With that, Zend can load the modules in your application, and just not models, but DbTable, Forms, Plugins, etc.
write the following in your bootstrap file:
protected function _initDefaultModuleAutoloader()
{
$resourceLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH,
));
return $resourceLoader;
}
in your models folder create new file and name it "Data.php"
in the Data.php declare the class like this:
class Model_Data extends Zend_Db_Table_Abstract {.....}
you can now instantiate your data model like so:
$data = new Model_Data();
good luck :-)
in your application ini you should have
autoloadernamespaces.0 = 'Application' instead of appnamespace
then your model would be in
/library/Application/Model/Data.php
but why dont you use the default "models" folder in the suggested application structure.
I had forgotten to add the file extension .php to the file, just in case someone else makes the same mistake
Related
I have an auth plugin working. I am trying to add ACL to it according to the excellent video series at http://www.youtube.com/watch?v=b6qsSnLfcmE&feature=relmfu.
My problem is that when I try to register the model in Bootstrap so that I can pass the instance to the plugin, I get a server 500 error. My bootstrap looks like this...
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$modelLoader = new Zend_Application_Module_AutoLoader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
$acl = new Model_SystemAcl;
$auth = Zend_Auth::getInstance();
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_AccessCheck($acl,$auth));
return $modelLoader;
}
}
It is the line:
$acl = new Model_SystemAcl;
That is causing the problem. If I comment it out (and the $acl parameter that is passed) it works fine. It appears as though somehow my system is not configured properly to load models. This is the entire Bootstrap shown in the tutorial btw. Perhaps there is something in Application.ini I need?
EDIT: Yes, SystemAcl.php exists and is in [applicationdir]/models
This is a full example for load models from the application namespace "Application"
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => 'Application',
)
);
$resourceLoader->addResourceType('model', 'models/', 'Model');
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader($resourceLoader);
Try to instantiate resources that may not yet have loaded i not a good practice.
You should use an Controller Plugin instead.
Based on your setup the filename of your class should be SystemAcl.php, not Model_SystemAcl.php.
if it is in application/models then i would have thought the script should be Models_SystemAcl not Model_SystemAcl (no 's'). Saying that, it is better to use plugins in the long run, rather than sticking this sort of stuff in the bootstrap. Those tutorials are good though :)
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 .
I have a very strange case where my Module is working but my Module's boostrap is not being loaded.
Here is the segment in my application.ini for module autoloading:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
Here is the bootstrapper:
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'User_',
'basePath' => APPLICATION_PATH .'/modules/user',
'resourceTypes' => array (
'model' => array(
'path' => 'models',
'namespace' => 'Model',
)
)
));
}
Structure of my modules
Application
--modules
----user
------config/
------controllers/
------models/
------views/
------Bootstrap.php
----admin
The problem here is that User_Bootstrap is not being loaded.
<?php
class User_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoload()
{
Zend_Registry::set('debug', 'haha');
}
}
By doing a Zend_Registry::get('debug') on any controller, it doesn't recognize that the key was set in the module bootstrap. In fact any syntax error in the User_Bootstrap does not work.
I don't know why User_Bootstrap is not being autoloaded. This is driving me crazy because I've been researching for 5 hours and can't even get a blog post close to covering this case...
Speaking of which, my models and controller classes are being autoloaded fine.
Try the following...
Change your application.ini file to use
; lose the quotes
resources.modules[] =
See http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.modules
Remove the _initAutoload() method from your Application Bootstrap class. You don't need this as the module bootstrap will automatically create a resource loader for your User_ classes
Not sure but it might as simple as improper case.
--Modules is in your structure but you keep referring to it as /modules. These should match case.
I hope it's that simple.
Don't duplicate the function names of your main bootstrap in your module bootstrap, as far as I know in ZF 1.x all of the boostraps get processed every call and I think your _initAutoload in the main boostrap is overriding the module bootstrap.
try calling your function some different like _initModuleAutoload.
At least worth a shot :)
Have you tried disabling frontController directory in application.ini config file? Try commenting/deleting this line:
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
Thanks for previous replies..
I am trying to print Hello_world using zend framework. I wrote php file in model folder and return string value as a "Hello_world". In controller i access the value of PHP like this
$value = new TextReturner();
$this->view->setValue = $value->hello_world(); . i dont know how to access the value from controller to the view php file. I am new to zend framework. I already go through the outline structure of zend framework, i dont know how to access through codings. If anyone have idea of how to print hello_world through MVC pls guide me.
You are trying to use class $value = new TextReturner(); but your controller doesn't see that class.
Set this in your Bootstrap file that will help:
protected function _initAutoLoad() {
// Add autoloader empty namespace
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
),
),
)
);
return $resourceLoader;
}
This will be autoload all of your model class.
in view you can access your variable like this:
<?php echo $this->setValue;?>
I'm new to zend framework so maybe this question is stupid..
I've got a default hierarchy
site
|--bootstrap.php
|--application
|--models
|-- Item.php
|-- ModelAbstract.php
|--...
Inside Item.php there's
<?php
//TODO: trying to remove this require...
require_once('ModelAbstract.php');
class CF_Model_Flower extends CF_Model_Abstract
{
...
Inside 'ModelAbstract.php' there's
<?php
class CF_Model_Abstract
{
...
And my application Bootstrap.php looks like
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'CF',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
...
If I removed the 'require_once' inside Item.php I get
Fatal error: Class 'CF_Model_Abstract' not found in /Mysite/application/models/Item.php on line 6
Why ? And how can I use autoloading to live without this require_once ?
In fact, renaming 'ModelAbstract.php' to 'Abstract.php' works. Can someone explain me why ?
Thx
I'm not familiar with Zend_Application_Module_Autoloader and such. But if they work anything like earlier ZF autoloading mechanisms, the autoloader will look for class CF_Model_Abstract in:
CF/Model/Abstract.php
or maybe with this namespace/basePath configuration in:
models/Model/Abstract.php
or:
models/CF/Model/Abstract.php
but probably not in:
models/ModelAbstract.php
So in other words, the underscores represent child directories.
Try specifying the _ in the namespace.
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'CF_',
'basePath' => dirname(__FILE__),
));
Put the following in your bootstrap file:
protected function _initAutoLoad()
{
$loader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => 'CF',
));
$loader->addResourceType('form', 'forms', 'Form')
->addResourceType('model', 'models', 'Model')
->addResourceType('mapper', 'models/mappers', 'Model_Mapper')
->addResourceType('dbtable', 'models/DbTable', 'Model_DbTable');
return $loader;
}
This will load models, forms, dbtables, and mappers for you. Hope this does the trick.