Zend framework route - zend-framework

I have the following controllers in my modules..
UserController.php
AdminUserController.php
Now the route for admin controller goes to: module/admin-user/ (default behaviour)
How do I make a route so all admin- will be changed to:
/admin/module/user

You will have to write a custom route.
In code:
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$route = new Zend_Controller_Router_Route(
'admin/:module/user', array('controller' => 'admin-user'));
$router->addRoute('module-admin-router', $route);
In a .ini file (I like to keep mine separate from application.ini):
[routes]
routes.module-admin-router.type = "Zend_Controller_Router_Route"
routes.module-admin-router.route = "archive/:module/user"
routes.module-admin-router.defaults.controller = "admin-user"
Then you will have to bootstrap that application section to enable the routes;
protected function _initRoutes ()
{
// setup routes here.
$front = $this->getResource('frontcontroller');
$router = $front->getRouter();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'routes');
$router->addConfig($config->routes);
}
This route will match any admin/module/user request and send it to the AdminUserController within the matching module.
Something like that should work. Now if things get really complicated, you'll probably need to dig into the regex router - but this is as simple as I can think of it needing to be.

Related

How to set router for earch module in zend framework

I have 1 zend (v1) application, and 2 module : default + admin
I want when call default module will be set router in configs/router/default.ini
and if in module admin do not any thing
I tried using plugin but it doesn't work
in my plugin
class Australian_Controller_Plugin_DefaultRouter extends Zend_Controller_Plugin_Abstract {
public function routeShutdown(Zend_Controller_Request_Abstract $request) {
$currModule = $request->getModuleName();
if ($currModule != 'default') {
return;
}
$fontController = Zend_Controller_Front::getInstance();
$router1 = new Zend_Controller_Router_Rewrite();
$fontController->getRouter()->removeDefaultRoutes();
$myRoutes = new Zend_Config_Ini(APPLICATION_PATH . '/configs/router/default.ini', 'production');
$router1->addConfig($myRoutes, 'routes');
$fontController->setRouter($router1);
}
}
and /default/Bootstrap.php
protected function _initRoutes() {
$fontController = Zend_Controller_Front::getInstance();
$fontController->registerPlugin(new Australian_Controller_Plugin_DefaultRouter());
}
thanks
Note that you are adding new router after Routing so Zend already decoded address using old routes. This way you can generate URLs using new routes, but they will not e recognized by Zend. You need to call $router->route($request); again to set module/controller/action using new set of routes.
Sadly this is not working when its called in routeShutdown and has to be added to preDispatch(). Sadly I'm quite new to Zend too and still not grasping why it is so.
Code i used:
$fontController = Zend_Controller_Front::getInstance();
$router = $fontController->getRouter();
$r = new Zend_Controller_Router_Route(
'/testnew',
array(
'module' => 'user',
'controller' => 'index',
'action' => 'myaccount',
));
$router->addRoute('testnew', $r);
$router->route($request);

How to rename module admin in Zend Framework

I have a problem with URL rewrite in Zend Framework, I hope someone helps me solve that.
I need to rename module admin as admindev in URL apply for all controller and action.
Here my code write in Bootstrap.php:
public function _initModuleRoutes()
{
$this->bootstrap('FrontController');
$frontController = $this->getResource('FrontController');
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
'admindev/:action/*',
array(
'module'=>'admin',
'controller'=>':controller',
'action'=>':action'
)
);
$router->addRoute('admin',$route);
return $router;
}
Thanks all,
You didn't specify the :controller parameter in the route.
Try it like that:
public function _initModuleRoutes()
{
$this->bootstrap('FrontController');
$frontController = $this->getResource('FrontController');
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
'admindev/:controller/:action/*',
array(
'module'=>'admin',
'controller'=>':controller',
'action'=>':action'
)
);
$router->addRoute('admin',$route);
return $router;
}
Also you can achieve the same effect with application.ini configuration:
resources.router.routes.admindev.type = "Zend_Controller_Router_Route"
resources.router.routes.admindev.route = "/admindev/:controller/:action/*"
resources.router.routes.admindev.defaults.module = "admin"
Other application.ini tips and tricks here

hide the index controller from the URL for a single module

After creating a modular structure for a single module would prevent the url appears the name of the controller.
everything works with the defaul
site.con/foo/index/action/
I wish I could write as
site.com/foo/action/
being IndexController the only controller that module.
I have tested several solutions but do not work. Being the first app with ZF I do not quite clear the steps to be taken.
You need Zend Routes.
Define routes in your
bootstrap.php
Open your bootstrap.php and put the following:
function _initRoutes() {
$front_controller = Zend_Controller_Front::getInstance();
$router = $front_controller->getRouter();
$router->addRoute('foo-action', new Zend_Controller_Router_Route(
'<foo module name>/<action name>', array('module' => 'foo', 'controller' => 'index', 'action' => '<action-name>')
));
}
PS:Worked / Didn't work?
Mention in comments and if didn't work, give proper names of module, controller and action.
EDIT:
How to set default controller / module in application.ini
routes.index.type = "Zend_Controller_Router_Route"
routes.index.route = "/"
routes.index.defaults.module = "<module name>"
routes.index.defaults.controller = "index"
routes.index.defaults.action = "index"
Solves it?

Prefix Folder structure for Zend

If i have a url such as this http://example.com/controller/action every thing works find. as expected. However i need to deploy this and in deployment things change a bit to htttp://deploy.com/stuff/pile/controller/action is there any way i can control this in zend.
Thanks in advance.
Add this route to your Bootstrap:
protected function _initRoute() {
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->addRoute(
'prefix_route',
new Zend_Controller_Router_Route('stuff/pile/:controller/:action',
array('controller' => $front->getRequest()->getControllerName(),
'action' => $front->getRequest()->getActionName())));
);
}

Zend Framework: Zend_translate and routing related issue

I have implemented Zend_Navigation, Zend_Translate in my application.
The routing is setup in Bootstrap.php like below.
$fc = Zend_Controller_Front::getInstance();
$zl=new Zend_Locale();
Zend_Registry::set('Zend_Locale',$zl);
$lang=$zl->getLanguage().'_'.$zl->getRegion();
$router = $fc->getRouter();
$route = new Zend_Controller_Router_Route(':lang/:module/:controller/:action/*',
array(
'lang'=>$lang, 'module'=>'default', 'controller'=>'index', 'action'=>'index'
));
$router->addRoute('default', $route);
$fc->setRouter($router);
$fc->registerPlugin( new Plugin_LanguageSetup());
in LaunguageSetup Plugin i have defined the dispatchLoopStartup method to do the checking of the language param
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
$this->createLangUrl($request);
$this->_language = $request->getParam('lang');
if ((!isset($this->_language)) || !in_array($this->_language, $this->_languagesArray)) {
$this->_language = 'en_US';
$request->setParam('lang', 'en_US');
}
$file = APPLICATION_PATH.$this->_directory.$this->_language.'.csv';
$translate = new Zend_Translate('csv', $file, $this->_language);
Zend_Registry::set('Zend_Translate', $translate);
$zl = Zend_Registry::get('Zend_Locale');
$zl->setLocale($this->_language);
Zend_Registry::set('Zend_Locale', $zl);
// $fc = Zend_Controller_Front::getInstance();
// $router = $fc->getRouter();
// $route = new Zend_Controller_Router_Route(':lang/:module/:controller/:action/*', array(
// 'lang'=>$this->_language, 'module'=>'default', 'controller'=>'index', 'action'=>'index'
// ));
// $router->addRoute('default', $route);
// $fc->setRouter($router);
}
What happen is the language always have the default value, the 'lang' param never default lang value in route, even if i type it in the address bar manually i.e /en_US/module/controller/action/ It always get revert back to the default Zend_locale();
Only way i can fix it is to setup the route again in the plugin and inject a correct language value as default. Any Idea why?
try and do a var_dump of the 2 vars ( _language, _languagesArray ) before this line
if ((!isset($this->_language)) || !in_array($this->_language, $this->_languagesArray)) {
I suspect that there it should be the problem, because you put yor plugin on dispatchLoopStartup, and then the params might not be populated, i put my plugin on routeShutdown see my implementation of the languange plugin.
Sort of a partial solution.
in dispatchLoopStartup
add
$fc = Zend_Controller_Front::getInstance();
$router = $fc->getRouter();
$router->setGlobalParam('lang',$this->_language);
better than redefine and overwrite the route again and 'fake' the language param by changing the default 'lang' value.
it's just less than perfect. Zend_router suppose to pick up the 'lang' param and have them placed in Zend_navigation->menu();