What is Route, where and how to define it? - zend-framework

I have several modules in my zend application. On one of the view script of my modules, I created a URL as such
$links['create'] = $this -> url(array("controller" => "roles", "action" => "create"), "custom");
This brings an error, saying Route "custom" is not define.
What is Route? Where to define it and How?

In my bootstrap file i have initialized my routing by adding following function
public function _initRouting() {
// Get Front Controller Instance
$front = Zend_Controller_Front::getInstance();
// Get Router
$router = $front->getRouter();
$routedetialevent = new Zend_Controller_Router_Route(
'/events/detail/:id',
array(
'controller' => 'events',
'action' => 'detail'
)
);
$routeregister = new Zend_Controller_Router_Route(
'/index/register/:id',
array(
'controller' => 'index',
'action' => 'register'
)
);
$routerdetail = new Zend_Controller_Router_Route(
'/commentaries/details/:id',
array(
'controller' => 'commentaries',
'action' => 'details'
)
);
$router->addRoute('post', $routedetialevent);
$router->addRoute('register', $routeregister);
$router->addRoute('detail', $routerdetail);
}
as i have added the custom route in my events, commentaries whenever i visit detail page i dont have to write id in my url so my url will be like
http://localhost/example/events/detail/3
If i wouldnt have added route than my url would be like
http://localhost/example/events/detail/id/3

The Zend Framework manual has pretty decent documentation about routes and the router, including descriptions of several ways to define routes.
At a very basic level, routes are used both to parse URLs into parameters (like which controller and action should be used), and to do the reverse: take parameters and produce a URL.
For your purposes, unless you want to change how ZF will build your URL, you can just drop the "custom" part off of your url call.

Related

Route sudmains controller with Zend

I have the following structure on my app:
Modules =>
default => site.com
blog => blog.site.com
admin => admin.site.com
I used this code on my bootstrap to allow subdomains and redirect to the follow module:
$pathRoute = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'index', 'action' => 'index'));
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$blogDomainRoute = new Zend_Controller_Router_Route_Hostname(
'blog.site.com', array(
'module' => 'blog',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('blogdomain', $blogDomainRoute->chain($pathRoute));
And the same code to adminDomainRoute.
It works fine! But now i notice that my pagination route don't work, i have the follow route to manage pages in admin module:
routes.usuarios.route = /usuarios/pagina/:pagina
routes.usuarios.defaults.module = admin
routes.usuarios.defaults.controller = usuarios
routes.usuarios.defaults.action = index
routes.usuarios.defaults.pagina = 1
I tried to change the route to
routes.usuarios.route = admin.site.com/usuarios/pagina/:pagina
But i still got action no found:
array (
'controller' => 'usuarios',
'action' => 'pagina',
'module' => 'admin',
)
How can i route admin.site.com/usuarios/pagina/1 admin.site.com/usuarios/pagina/3 ?
The thing that jumps at me from your setup is, that in the ini format (your current admin route), you are using the default router. Well this router knows nothing about the hostname you are on, so it is looking for an url like this one:
site.com/admin.site.com/usuarios/pagina/1 admin.site.com/usuarios/pagina/3
What you want is something like this:
//Create a hostname route. This route is only concerned with the subdomain part of the uri
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
'admin.:host.:domain');
//Create a default router that would take care of the rest of the routing.
$defaultRoute = new Zend_Controller_Router_Route(
'/usuarios/pagina/:pagina',
array(
'module'=>'admin',
'controller'=>'usuarios',
'action'=>'index'
)
);
//Chain those two routes together to make them go one after the other.
$defaultRoute=$hostnameRoute->chain($defaultRoute);
This code may need a bit of tweaking, but I think this should do what you need.

Zend Framework routing

I'm trying to setup some routes for my ZF app but not getting too far. I have a controller 'WebServiceController', it has an index action and a lookupTransaction action. I want to use routes like this:
ws/
ws/lookupTransaction
Ideally I'd like anything with a 'ws/' prefix to go to the WebServiceController and match the action name. I'm not sure how to do that yet but I am trying to get each route working so I added these two routes:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
function _initRoutes()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$router->addRoute('ws', new Zend_Controller_Router_Route('ws/', array(
'controller' => 'web-service',
'action' => 'index',
)));
$router->addRoute('ws/lookupTransaction', new Zend_Controller_Router_Route('ws/lookupTransaction', array(
'controller' => 'web-service',
'action' => 'lookup-transaction',
)));
}
}
The first one works as expected but the second one doesn't, I just get 'Application Error'. What am I doing wrong? Just out of interest, if I remove my two routes and try and go to:
web-service/lookup-transaction
I still get the same error!
Solved
Here is how I can make it work with camel cased action name and camel cased URL.
$router->addRoute('ws', new Zend_Controller_Router_Route('ws/:action', array(
'controller' => 'web-service',
'action' => 'index',
)));
$router->addRoute('ws-lookupTransaction', new Zend_Controller_Router_Route('ws/lookupTransaction', array(
'controller' => 'web-service',
'action' => 'lookup-transaction',
)));
Thanks
Ziad
Try just this one route as a solution to both problems:
$router->addRoute('ws', new Zend_Controller_Router_Route('ws/:action', array(
'controller' => 'web-service',
'action' => 'index',
)));
the action parameter then serves as a default, so if no action is specified in the URL, index will be used. Otherwise it will route to the action in the URL. So example.com/ws/lookupTransaction will go to lookuptransactionAction() in your controller.
If this still gives you an error, post the error message so we can see what the problem is.
The router actually transforms URLs to lowercase. So the correct URL should be all lowercase dash separated words. Also I'm not sure if it's possible to use slash in route name (the first parameter of addRoute()).

Zend framework admin module and other modules

I want to build a CMS based on Zend Framework for my needs which has admin module with authentication and other modules (pages, users, news) which can be used as plugin modules based on the appliaction needs.
I want every module to have specific frontend and backend code, so that it could be accessed like e.g. http://localhost/mycms/pages/view/5 to view a certain page from the pages module by calling Pages controller, view action. The backend for every plugin needs to be tied to the admin and require authentication, it could be accessed like http://localhost/mycms/admin/pages/add.
The problem is that the solution I found involves removing default routes and writing custom routing for every controller action inside the plugin modules like:
$router->removeDefaultRoutes();
$route = new Zend_Controller_Router_Route_Static(
'admin/pages/add',
array(
'module' => 'pages',
'controller' => 'Pages',
'action' => 'add'
)
);
$router->addRoute('pages_pages_add', $route);
$route = new Zend_Controller_Router_Route_Regex(
'pages/view/(\d+)',
array(
'module' => 'pages',
'controller' => 'Pages',
'action' => 'view'
),
array(
'1' => 'page_id'
)
);
$router->addRoute('pages_pages_view', $route);
Do you have any ideas how I can avoid this custom routing?
Have a look at Front Controller Plugins, they may give you more flexibility...
http://framework.zend.com/manual/en/zend.controller.plugins.html
class Controller_Plugin_Foo extends Zend_Controller_Plugin_Abstract
{
public function preDispatch( Zend_Controller_Request_Abstract $request )
{
$frontController = Zend_Controller_Front::getInstance();
...
}
}

Zend Framework Router Getting /module/VALUE/controller/action

I've been googling around and I can't seem to find anything which explains the use of ZF router well. I've read the documentation on the site, which seems to only talk about re-routing.
I am trying to make the format:
/module/value/controller/action give /module/controller/action passing on value as a parameter
e.g.
/store/johnsmithbigsale/home/newstuff would route to /store/home/newstuff passing on johnsmithbigsale as the value to a parameter with a hidden namespace e.g. storeName.
Some help would be greatful!
You can use Zend_Controller_Router_Route to map your url parts to modules, controllers, actions, and parameters that can be used in the controller by $this->_getParam('varName'). You can define these routes in the application.ini file or in the application bootstrap.
// custom city route
$route = new Zend_Controller_Router_Route(
'cities/:city',
array(
'controller' => 'city',
'action' => 'view'
)
);
$this->addRoute('city', $route);
// custom buy widgets route
$route = new Zend_Controller_Router_Route_Regex(
'buy_(.+)_widgets/([0-9]+)(.*)',
array(
'controller' => 'widgets',
'action' => 'view'
),
array(
1 => 'nothing',
2 => 'widget_id',
3 => 'vars'
)
);
$this->addRoute('widgets', $route);
The regex route is kind of specific to my app, but you can see that each match can get mapped to a parameter.

Using Zend_Controller_Router_Route to create friendly URLs for the actions in IndexController

In my IndexController, I currently have indexAction (homepage), loginAction and logoutAction. I'm trying to remove "/index/" from the URL to get domain.com/login instead of domain.com/index/login.
What is the cleanest way to achieve this? Is there a RegEx we can use? I don't ever want /index/ in the URL.
My current solution, which I believe can be improved upon, is below. Also, what does the first parameter in addRoute() do?
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initViewHelpers()
{
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->addRoute('login',
new Zend_Controller_Router_Route('login/*', array(
'controller' => 'index',
'action' => 'login'
))
);
$router->addRoute('logout',
new Zend_Controller_Router_Route('logout/*', array(
'controller' => 'index',
'action' => 'logout'
))
);
}
}
There is nothing to impove, you have to create route for every action. This will allow you to change route defaults (module/controller/action) without modifying your code.
First parameter is the route name, which you have to use with url() helper in your views:
Login
Update. You can use such route, if you want only one route without "index" in url:
$router->addRoute('default',
new Zend_Controller_Router_Route(':action/*', array(
'controller' => 'index',
))
);