Zend Translate Routing for custom modules - php

How do I have to set up the routing for a default language in Zend Framework on a custom module, in my case the admin module.
I have the following code:
$langRoute = new Zend_Controller_Router_Route(
':lang/admin',
array(
'lang' => 'ro',
)
);
what i wan't to obtain is url's like the following:
www.example.com/ro/admin/pages/add/62
www.example.com/ro/admin/pages/index/by/date_modified/order/asc
etc.

Try this will working:
protected function _initRoutes() {
$langRoute = Zend_Controller_Front::getInstance ()->getRouter ();
$langRoute->removeDefaultRoutes ();
$route = new Zend_Controller_Router_Route(
':lang/:module/:controller/:action/*',
array (
'lang' => 'ro',
'module' => 'admin',
'controller' => 'index',
'action' => 'index'
)
);
$langRoute->addRoute ( 'langrouter', $route );
}

Related

ZF1 Routing Not working for View Script Sub-directories

I am using ZF1 and am trying to figure out why the view/scripts sub directories routing is not working. Here is the code from the bootstrap adding the routes. Please let me know what I may have wrong. Thanks for your time.
public function _initRoutes()
{
$controller = Zend_Controller_Front::getInstance();
$router = $controller->getRouter();
//Route for user Account
$account = new Zend_Controller_Router_Route(
'ecommerce/account/:action',
array(
'module' => 'ecommerce',
'controller' => 'user_account',
'action' => 'index'
)
);
//Route for user Cart
$cart = new Zend_Controller_Router_Route(
'ecommerce/cart/:action',
array(
'module' => 'ecommerce',
'controller' => 'user_cart',
'action' => 'index'
)
);
//die(print_r($account));
$router->addRoute('ecommerce/user_account/', $account);
$router->addRoute('ecommerce/user_cart/', $cart);
}
you have to try this code
$router = Zend_Controller_Front::getInstance();
$router1 = $router->getRouter();
$router1->addRoute('category/:id/:name/*',
new Zend_Controller_Router_Route('category/:user_id/:user_name/*', array(
'controller' => 'user',
'action' => 'index'
))
);
This will help you.

Zend Custom Modules Routes

i have this set up:
application
---admin
-----controllers
-------IndexController.php
---public
-----controllers
---modules
-----users
-------controllers
-----pages
-------controllers
I'd like to have this routes :
www.domain.com/admin/modulename/controller/action/
or if module not exist i use admin controllers
www.domain.com/admin/controller/action/
Any suggesiton?
Thanks
In your bootstrap file write this code :
$router = Zend_Controller_Front::getInstance()->getRouter();
if($moduleName){
$route = new Zend_Controller_Router_Route(
':module/:controller/:action/',
array(
'controller' => $ControllerName,
'module' => '$moduleName',
'action' => $ActionName
)
);
}else{
$route = new Zend_Controller_Router_Route(
':controller/:action/',
array(
'controller' => $ControllerName,
'action' => $ActionName
)
);
}
$router->addRoute('default', $route);

SocialEngine module with custom routes

I'm developing a module for the SocialEngine package and I'd like to be able to specify multiple custom routes.
Currently I'm editing the Bootstrap.php file found in the directory of my module with the following;
class Courses_Bootstrap extends Engine_Application_Bootstrap_Abstract
{
protected function _initRouter(){
$fc = Zend_Controller_Front::getInstance();
$router = $fc->getRouter();
$router->addRoute('courses', new Zend_Controller_Router_Route('courses/activity/:activity_id', array('module' => 'courses', 'controller' => 'index','action' => 'activity')));
$router->addRoute('courses', new Zend_Controller_Router_Route('courses/course/edit/:course_id', array('module' => 'courses', 'controller' => 'course','action' => 'edit')));
$router->addRoute('courses', new Zend_Controller_Router_Route('courses/course/create/:course_id', array('module' => 'courses', 'controller' => 'course','action' => 'create')));
return $router;
}
}
However it would seem that when I specify more then 1 route all routes stop passing in the custom variable (course_id or activity_id)
I'm retrieving the variable as follows;
$course_id = $this->getRequest()->getParam("course_id");
I've taken the approach from here;
http://tjgamble.com/2011/04/adding-custom-routes-to-your-socialengine-4-modules/
Many thanks,
Andy
you have to give them different names:
$router->addRoute('courses_activitiy', new Zend_Controller_Router_Route('courses/activity/:activity_id', array('module' => 'courses', 'controller' => 'index','action' => 'activity')));
$router->addRoute('courses_course', new Zend_Controller_Router_Route('courses/course/edit/:course_id', array('module' => 'courses', 'controller' => 'course','action' => 'edit')));
$router->addRoute('courses_create', new Zend_Controller_Router_Route('courses/course/create/:course_id', array('module' => 'courses', 'controller' => 'course','action' => 'create')));

How to redirect requests to www.example.com/admin to different module in Zend

I have a problem with redirecting requests in my application.
My rules:
employer.domain.com - should point to a page of the employer - uses default module
employer.domain.com/panel/ - should point to a administration page of specific employer - uses dashboard module
www.domain.com - should point to page aggregating all employers - uses default module
I've tested a lot of different routes, but when one route is working, other get broken. Also often it works only for root paths, but when I add call to some controller and action - it crashes. Maybe I should write custom controller plugin? What do You think?
Here is my current configuration. It's a mess, but maybe it will help catching some silly mistake.
// employer.domain.com/panel
$pathRoute_panel = new Zend_Controller_Router_Route(
':panel/:controller/:action/:id/',
array(
'panel' => '',
'module' => 'dashboard',
'controller' => 'index',
'action' => 'index',
'id' => '',
),
array(
'panel' => 'panel'
)
);
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
':employer.'.$config['host'],
null,
array(
'employer' => '([a-z0-9]+)',
)
);
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel));
// employer.domain.com - main employer page
$pathRoute_panel = new Zend_Controller_Router_Route(
'',
array(
'module' => 'default',
'controller' => 'vcard',
'action' => 'index',
'id' => '',
)
);
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
':employer.'.$config['host'],
null,
array(
'employer' => '([a-z0-9]+)',
)
);
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel));
// domain.com/
$pathRoute = new Zend_Controller_Router_Route_Module(
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
),
$dispatcher,
$request
);
$route = new Zend_Controller_Router_Route_Hostname($config['host']);
$router->addRoute('default', $route->chain($pathRoute));
// www.domain.com/
$pathRoute = new Zend_Controller_Router_Route_Module(
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
),
$dispatcher,
$request
);
$route = new Zend_Controller_Router_Route_Hostname('www.'.$config['host']);
$router->addRoute('default_www', $route->chain($pathRoute));
EDIT: This is my solution:
// employer.domain.com
$pathRoute_panel = new Zend_Controller_Router_Route_Module(
array(
'module' => 'default',
'controller' => 'vcard',
'action' => 'index',
'id' => '',
)
);
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
':employer.'.$config['host'],
null,
array(
'employer' => '([a-z0-9]+)',
)
);
$router->addRoute('employer_vcard', $subdomainRoute->chain($pathRoute_panel));
// employer.domain.com/panel/
$pathRoute_panel = new Zend_Controller_Router_Route(
'panel/:controller/:action/:id/',
array(
'panel' => 'panel',
'module' => 'dashboard',
'controller' => 'index',
'action' => 'index',
'id' => '',
)
);
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
':employer.'.$config['host'],
null,
array(
'employer' => '([a-z0-9]+)',
)
);
$router->addRoute('employer_panel', $subdomainRoute->chain($pathRoute_panel));
// Enforce Subdomain usage
// Will match employer.domain.com/*
$subdomainRoute = new Zend_Controller_Router_Route_Hostname(
'employer.'.$config['host'] . '/*'
);
// Will match /panel/{id, required}/{controller, optional}/{action, optional}/{further parameters, optional}
$pathRoute_panel = new Zend_Controller_Router_Route(
'panel/:id/:controller/:action/*',
array(
'module' => 'dashboard',
'controller' => 'index',
'action' => 'index'
),
array(
'id' => '\d+'
)
);
// employer.domain.com - main employer page
// will match everything not matched before!
$pathRoute_page = new Zend_Controller_Router_Route(
'*',
array(
'module' => 'default',
'controller' => 'vcard',
'action' => 'index'
)
);
// You can add several routes to one chain ;) The order does matter.
$subDomainRoute->chain($pathRoute_page);
$subDomainRoute->chain($pathRoute_panel);
$router->addRoute($subDomainRoute);
Annotations are in the code. You can use the wildcard (*) to match anything further! There's no need of the default_www route - this's just the default behaviour (the default route now will match every subdomain but employer as it is matched by the subDomainRoute).

Syntax for Zend_Controller_Router_Route with infinite parameters?

I need a Zend_Controller_Route_Route with controller => 'downloads', action => 'index' and a parameter variable which is infinite.
www.domain.com/downloads/this/is/the/path/to/the/folder
should redirect to
DownloadsController > indexAction
I was thinking about something like the following:
'downloads' => array(
'route' => 'downloads/*',
'defaults' => array('controller' => 'downloads', 'action' => 'index')
But how do I request the * as a parameter?
You can use a RegEx Route http://framework.zend.com/manual/de/zend.controller.router.html#zend.controller.router.routes.regex
$router = Zend_Controller_Front::getInstance()->getRouter(); // returns a rewrite router by default
$route['default'] = new Zend_Controller_Router_Route_Regex(
'/downloads/(.*)',
array(
'controller'=> 'Downloads',
'action' => 'index',
),
array(
1 => 'downloadpath',
)
);
$router->addRoute('default', $route['default']);
I havent tested it but something like that shoudl work

Categories