Zend Framework - How does a controller point to a model? - zend-framework

This is a problem that I cannot figure out after hours of googling, the Zend Framework documentation didn't explain what i needed to know.
I have a file: /application/controllers/RegisterController.php
class RegisterController extends Zend_Controller_Action
{
}
How do I point this file to use classes from: /application/models/User.php
class Application_Model_User
{
}
The code works great when the file is named Register.php
What configurations do i need to make to point a controller to a specific model?

In your bootstrap, you can start by declaring your namespace:
protected function _initAutoload() {
//Autoloader
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
//Tells the application where to find the models
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
)
)
));
return $autoLoader;
}
Your model: /application/models/User.php
class Model_User extends Zend_Db_Table_Abstract
{
}
Then in you controller: /application/controllers/RegisterController.php
class RegisterController extends Zend_Controller_Action
{
public function indexAction() {
$model = new Model_User();
}
}

Related

PHP Fatal error: Class 'library\App\App_TestClass' not found in D:\xampp\htdocs\dev.gamenomad.com\application\controllers\ParticipantController.php

I'm new to zend framework. I'm trying to write a custom class. Here is The code:
<?php
namespace library\App;
class App_TestClass {
private $name;
function __construct(){
$this->name = 'My name is Test Class';
}
public function getName()
{
return $this->name;
}
}
?>
This class is located in library/App folder. I have also added this code to Bootstrap.php file:
public function _initAutoload()
{
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => 'library',
'namespace' => 'App'));
}
When I instantiate this class in ParticipantController.php I get this error: PHP Fatal error: Class 'library\App\App_TestClass' not found in D:\xampp\htdocs\dev.gamenomad.com\application\controllers\ParticipantController.php
I know this question has been asked before. But I couldn't solve my problem. Any help is appreciated in adveance.
Seems to me that you are mixing namespace with path in class name.
For ZF1 use just:
class App_TestClass {
delete - namespace library\App;
and change to:
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH .'library',
'namespace' => 'App_'));

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

Class Not Found in Zend Framework

I have installed a fresh copy of zend framework 1.11.11 on my local workstation (Windows). For my admin module I have created "Login.php" form under /application/modules/admin/models/Form/Login.php I have also set up autoloader in Bootstrap.php Like
protected function _initAutoloader()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('My_');
new Zend_Application_Module_Autoloader(array(
'basePath' => APPLICATION_PATH,
'namespace' => 'Default')
);
$loader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH.'/models/',
'namespace' => '')
);
$loader->addResourceType('forms', 'Form/', 'Form');
return $autoloader;
}
on my loginAction() method of IndexController.php file of admin module, i am using
$form = new Admin_Model_Form_Login();
But Getting below error:-
Fatal error: Class 'Admin_Model_Form_Login' not found in
C:\wamp\www\ztest\application\modules\admin\controllers\IndexController.php
Here is the code of Login.php
class Admin_Model_Form_Login extends Zend_Form
{
public function init()
{
parent::init();
$this->setAction('/admin/index/login')->setMethod('post');
$account = new Zend_Form_Element_Text('account');
$account->setLabel('Username')->setRequired(true);
$account->setOrder(1);
$this->addElement($account);
$password = new Zend_Form_Element_Password('password');
$password->setLabel('Password');
$password->setOrder(2);
$this->addElement($password);
$submit = new Zend_Form_Element_Submit('login');
$submit->setLabel('Login');
$submit->setOrder(3);
$this->addElement($submit);
}
}
Have you added a Bootstrap.php file to your module's path?
The file should be found in /application/modules/admin/Bootstrap.php
Similar to this:
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap
{
//Can be left blank
}

Zend Framework: How to 301 redirect old routes to new custom routes?

I have a large list of old routes that I need to redirect to new routes.
I am already defining my custom routes in the Bootstrap:
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$oldRoute = 'old/route.html';
$newRoute = 'new/route/*';
//how do I add a 301 redirect to the new route?
$router->addRoute('new_route',
new Zend_Controller_Router_Route($newRoute,
array('controller' =>'fancy', 'action' => 'route')
));
}
How can I add routes that redirect the old routes using a 301 redirect to the new routes?
I've done it like this
Add a Zend_Route_Regexp route as the old route
Add Controller and action for the old route
Add logic to parse old route
Add $this->_redirect($url, array('code' => 301)) for this logic
Zend Framework does not have this type of functionality built in. So I have created a custom Route object in order to handle this:
class Zend_Controller_Router_Route_Redirect extends Zend_Controller_Router_Route
{
public function match($path, $partial = false)
{
if ($route = parent::match($path, $partial)) {
$helper = new Zend_Controller_Action_Helper_Redirector();
$helper->setCode(301);
$helper->gotoRoute($route);
}
}
}
Then you can use it when defining your routes:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initCustomRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route_Redirect('old/route/*', array('controller'=>'content', 'action'=>'index'));
$router->addRoute('old_route', $route);
}
}
In controller, try this way:
$this->getHelper('redirector')->setCode(301);
$this->_redirect(...);
There are a few approaches to solve this issue.
The easiest is probably to just use your .htaccess file to do a RewriteRule pattern substitution [R=301]
You could also detect what route was used in the controller and redirect based on that:
public function preDispatch() {
$router = $this->getFrontController()->getRouter();
if ($router->getCurrentRouteName() != 'default') {
return $this->_redirect($url, array('code'=>301));
}
}
The way I used to do it was to redirect to a controller that only handled redirects. Now I use the custom class mentioned in my other answer.
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
//new routes
$router->addRoute('myroute',
new Zend_Controller_Router_Route_Static('/new/route/1234',
array('controller' =>'brands', 'action' => 'view', 'id' => '4')
));
//old routes
$oldRoutes = array(
'/old/route/number/1' => '/new/route/1234',
}
foreach ($oldRoutes as $oldRoute => $newRoute) {
$router->addRoute($oldRoute, new Zend_Controller_Router_Route_Static($oldRoute, array('controller' =>'old-routes', 'action' => 'redirect', 'new-route' => $newRoute)));
}
}
}
And the controller:
class OldRoutesController extends Zend_Controller_Action
{
public function redirectAction()
{
$newRoute = $this->_getParam('new-route');
return $this->_redirect($newRoute, array('code' => 301));
}
}

calling zend form to AuthenticationController error

this is my form script: located in application/forms and i copy it form here and by the way im using xampp
class Form_LoginForm extends Zend_Form
{
public function init()
{
$username = $this->addElement('text', 'username', array(
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'Alpha',
array('StringLength', false, array(3, 20)),
),
'required' => true,
'label' => 'Your username:',
)); ect.////
}
and this my authentication script.. located in application/controller:
class AuthenticationController extends Zend_Controller_Action {
public function loginAction()
{
$form = new Form_LoginForm(); // doest work
$this->view->form = $form;
$myDb = $this->getAuthAdapter();
$userName = 'user';
$password = 'ds';
$myDb->setIdentity($userName)
->setCredential($password);
}
private function getAuthAdapter(){
$myDb = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
$myDb->setTableName('zuser')
->setIdentityColumn('table1')
->setCredentialColumn('table2');
return $myDb;
}
}
i want to call the class form Form_LoginForm inside the AuthenticationController but it gives me and error: *Fatal error: Class 'Form_LoginForm' not found in C:\xampp\htdocs\zendframework\sampleSite\application\controllers\AuthenticationController.php on line 18*
my question is what is the right way to call a class forms.. and where is the __autoload located?
Try changing class Form_LoginForm extends Zend_Form into class Application_Form_LoginFrom extends Zend_Form and then in you're AuthController $form = new Application_Form_LoginForm