How mvc works in Zend framework - zend-framework

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

Related

Zend: Using forms in modules

I'm trying to use forms with modules, they should be stored inside the module. So at first my filestructure:
application/
(...other directories)
modules/
group/
controllers/
IndexController.php
(...controllers)
forms/
Create.php
views/
scripts/
(...view scripts)
Bootstrap.php
Within the IndexController, I'm trying to set the Form by
new Group_Form_Create()
and the class in Create.php is of course Group_Form_Create. I get the following error message:
Fatal error: Class 'Group_Form_Create' not found in (...)\application\modules\group\controllers\IndexController.php on line 380
The Bootstrap.php with the class Group_Bootstrap is just an empty class.
Actually, I'm using the default Zend structure, but it woun't work anyway. Any ideas wheres the problems or what could be a possible solution?
In my module bootstrap (APPLICATION_PATH/modules/group/Bootstrap.php), if use the following code:
//Loads the autoloader resources
$this->_moduleName = 'group';
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH ."/modules/".$this->_moduleName."/",
'namespace' => '',
'resourceTypes' => array(
//Tells the application where to find the forms
'form' => array(
'path' => 'forms/',
'namespace' => ucfirst($this->_moduleName).'_Form_'
),
//Tells the application where to find the models
'model' => array(
'path' => 'models/',
'namespace' => ucfirst($this->_moduleName).'_Model_'
)
)
));
I then call the forms or models like this:
$frm = new Group_Form_Create();
I use the same snippet in all my modules and I only change the value of the $this->_moduleName; each time.
Hope this helps !
It sounds like your module bootstraps are not being run. These are triggered by the module resource, which is loaded if you have:
resources.modules[] = ""
in your application.ini. So add this if it is not present.
Ideally, it should work out of box.
Add this in your bootstrap:
protected function _initAutoload() {
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Group_',
'basePath' => dirname(__FILE__),
));
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
return $autoloader;
}

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 custom form validator 'NOT FOUND'

I am stuck in the follow situation. To check a url with zend_form, I have to add a custom validator. I try to add the custom validator named 'IsUrl.php' in;
What I do now
I add IsUrl.php to;
Library/Lib/Validate/
In my boodstrap:
protected function _initLibAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Lib',
'basePath' => dirname(__FILE__),
));
return $autoloader;
}
Test in Controller by;
$test = new Zend_Validate();
$test = new Lib_Validate_IsUrl();
Fatal error;
Fatal error: Class 'Lib_Validate_IsUrl' not found in
Thanks in advice.
With kind regards,
Nick
You will have to tell ZF, that you have custom validators :) You could adjust your bootstrap like this:
protected function _initValidators () {
$autoloader = new Zend_Application_Module_Autoloader (array ('namespace' => '', 'basePath' => APPLICATION_PATH));
$autoloader->addResourceType ('Validator', 'validators', 'Validator_');
}

Model class is not found in zend framework project (quickstart)

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

Zend Framework autoloading classes extended by models

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.