Class Not Found in Zend Framework - 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
}

Related

how to make the site multilingual with cookie in zend..i already created with the route method now

Hello i have a multilingual setup in zend with the my custom created zend plugin but now the requirement is changes and i want to make the site without the params like lang/**
Here the plugin.
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$lang = $request->getParam('lang', null);
$translate = Zend_Registry::get('Zend_Translate');
if ($translate->isAvailable($lang)) {
$translate->setLocale($lang);
} else {
$translate->setLocale('en');
}
// Set language to global param so that our language route can
// fetch it nicely.
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->setGlobalParam('lang', $lang);
}
and in my bootstrap i make this...
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initTranslate() {
$translate = new Zend_Translate('gettext', APPLICATION_PATH . "/langs/", null, array('scan' => Zend_Translate::LOCALE_DIRECTORY, 'disableNotices' => true));
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Translate', $translate);
}
public function _initRoutes() {
$this->bootstrap('FrontController');
$this->_frontController = $this->getResource('FrontController');
$router = $this->_frontController->getRouter();
$langRoute = new Zend_Controller_Router_Route(
':lang/', array(
'lang' => 'en',
)
);
$defaultRoute = new Zend_Controller_Router_Route(
':controller/:action', array(
'module' => ':module',
'controller' => 'index',
'action' => 'index'
)
);
$defaultRoute = $langRoute->chain($defaultRoute);
$router->addRoute('langRoute', $langRoute);
$router->addRoute('defaultRoute', $defaultRoute);
}
protected function _initLanguage() {
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Application_Plugin_Language());
}
}
Dont know how to remove that lang/fr and lang/en and do it with the COOKIE can any one help on this??
PS: Zend version is 1.12

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

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

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

Change Layout in Bootstrap

I am new with zend and I have a problem changing my layout in the bootstrap. I want to change my layout when the user is logged in.
My function to change the layout in the bootstrap is like this:
protected function _initAuthState()
{
$layout = new Zend_Layout;
$layout->setLayoutPath('/layouts/scripts');
if (Zend_Auth::getInstance()->hasIdentity()):
// Logged in.
$layout->setLayout(layout2);
else:
// Not Logged in.
$layout->setLayout(‘layout’);
endif;
}
This code doesn't work, the layout is always the same ... help!
You are modifying a new layout instance, not the instance that is being used by the system.
I assume you are specifying your layout params in application.ini. So you need:
$this->bootstrap('layout');
$layout = $this->getResource('layout');
Then perform your check/modification on this layout instance.
BTW, changing layout is often done using a front-controller plugin. Still runs early enough to do the job, but is often more configurable and re-usable. See here and here for two examples.
I found the answer!!
this is my final result, and is working!!
Bootstrap.php:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initLoader(){
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => '../application/',
'namespace' => 'My',
));
$resourceLoader->addResourceTypes(array(
'plugin' => array(
'path' => 'plugins/',
'namespace' => 'Plugin',
)
));
}
public function _initPlugins()
{
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new My_Plugin_Layout());
}
}
application/plugins/Layout.php:
<?php
class My_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
public function preDispatch()
{
$user = Zend_Auth::getInstance();
$role = $user->getIdentity()->role;
$layout = Zend_Layout::getMvcInstance();
switch ($role) {
case 'admin':
$layout->setLayout('layout2');
break;
case 'normal':
$layout->setLayout('layout');
break;
default:
$layout->setLayout('layout');
break;
}
}
}
?>

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