Default routing in Zend Framework after defining some routes - php

I've been writing a project since some time and I've used the default routing, the :module\:controller:\:action.
After some time, I've added some routers to my config like:
resources.router.routes.page.route = "page/:slug"
resources.router.routes.page.defaults.module = "default"
resources.router.routes.page.defaults.controller = "pages"
resources.router.routes.page.defaults.action = "view"
resources.router.routes.page.defaults.slug = ""
But, after that, when I click on some link generated by view URL helper with one of the new routes all other links ignore some of given paramters. Example, I've got route:
resources.router.routes.project.route = "project/:slug"
resources.router.routes.project.defaults.module = "projects"
resources.router.routes.project.defaults.controller = "projects"
resources.router.routes.project.defaults.action = "view"
resources.router.routes.project.defaults.slug = ""
If I go to a link /project/test then link like this:
$this->url(
array('module' => 'admin', 'action' => 'list-users', 'controller' => 'users')
, null,true
);
will point to "/project"
Is there any possibility to maintain the default routing on top of custom routes? Can I add some default router that will work the same as the default one? It's probably something simple but I maybe missed the point. Thanks for all the help.
I've added something like this but with no effect:
resources.router.routes.default.route = ":module/:controller/:action"
resources.router.routes.default.defaults.module = "default"
resources.router.routes.default.defaults.controller = "pages"
resources.router.routes.default.defaults.action = "view"
resources.router.routes.default.defaults.slug = ""

In order for you to set your custom routing, you need to get the router component and pass your routes into it.
This is how I did mine in a project I am working on. In your Bootstrap class, you create the following function
protected function _initRoutes()
{
$this->bootstrap('frontcontroller');
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$myRoutes = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini','production');
$router->addConfig($myRoutes,'routes');
This calls the front controller and gets the router from it. I then pass my routes config into it.
I hope this answers your question.

The problem is that the options for url helper are as follows:
url($urlOptions, $name, $reset)
Therefore, when you set $name to null, current route ('project') is used. Not event setting $reset to true will help. Replace null with 'default' and it should work.

Related

Zend Framework - setting up user-friendly URLs with routes and regex

I have two issues with user-friendly URLs.
I have a router set up as follows:
The actual URL is http://www.example.com/course/view-batch/course_id/19
I want a friendlier URL http://www.example.com/java-training/19
I have setup the following route in application.ini:
resources.router.routes.viewbatchcourse.route = "/:title/:course_id/"
resources.router.routes.viewbatchcourse.defaults.controller = course
resources.router.routes.viewbatchcourse.defaults.action = view-batch
resources.router.routes.viewbatchcourse.reqs.course_id = "\d+"
This works perfectly well.
Now I have a new page - which contains user reviews for Java
The actual URL is http://www.example.com/course/view-reviews/course_id/19
I want a friendlier URL http://www.example.com/java-reviews/19
I realize its not possible because one route is already setup to match that format.
So I was thinking if its possible to use regex and check if title contains "reviews" then use this route.
I tried this approach, but it doesn't work. Instead, it opens the view-batch page:
resources.router.routes.viewreviews.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.viewreviews.route = "/:title/:course_id"
resources.router.routes.viewreviews.defaults.controller = "course"
resources.router.routes.viewreviews.defaults.action = "view-reviews"
resources.router.routes.viewreviews.reqs.course_id = "\d+"
resources.router.routes.viewreviews.reqs.title = "\breviews\b"
The closest I have got this to work is
resources.router.routes.viewreviews.route = "/:title/:course_id"
resources.router.routes.viewreviews.defaults.controller = "course"
resources.router.routes.viewreviews.defaults.action = "view-reviews"
resources.router.routes.viewreviews.reqs.course_id = "\d+"
resources.router.routes.viewreviews.reqs.title = "reviews"
Now if I enter the URL http://www.example.com/reviews/19, then the view-reviews action gets called.
Is it possible - to check if title contains the word "reviews" - then this route should be invoked?
Going back to my earlier working route for http://www.example.com/java-training/19:
resources.router.routes.viewbatchcourse.route = "/:title/:course_id/"
resources.router.routes.viewbatchcourse.defaults.controller = course
resources.router.routes.viewbatchcourse.defaults.action = view-batch
resources.router.routes.viewbatchcourse.reqs.course_id = "\d+"
The number 19 is the course id, which I need in the action to pull the details from the database.
But when the page is displayed, I dont want the number 19 visible.
I just want the URL to be http://www.example.com/java-training
Is this possible?
1) You can use Route_Regex to achieve what you want
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route_Regex(
'([a-zA-Z]+)-reviews/(\d+)',
array(
'module' => 'default',
'controller' => 'course',
'action' => 'view-reviews'
),
array(
1 => 'language',
2 => 'course_id',
)
);
$router->addRoute('review', $route);
$route = new Zend_Controller_Router_Route_Regex(
'([a-zA-Z]+)-training/(\d+)',
array(
'module' => 'default',
'controller' => 'course',
'action' => 'view-batch'
),
array(
1 => 'language',
2 => 'course_id',
)
);
$router->addRoute('training', $route);
}
2) For the second point I can't see how it can be possible as is.
One thing you could do though is to use the name of the course, if you have one, to display an url like :
www.xyz.com/java-training/my-awesome-course-19
www.xyz.com/java-training/19/my-awesome-course
It would be pretty easy using the routes i mentionned above.
for question 1. I think you can solve this problem quite simply by altering the route. You don't need to have :title as part of the route, instead it can be hard coded in your case. I would recommend the following configuration.
resources.router.routes.viewbatchcourse.route = "/java-training/:course_id/"
resources.router.routes.viewbatchcourse.defaults.controller = course
resources.router.routes.viewbatchcourse.defaults.action = view-batch
resources.router.routes.viewbatchcourse.defaults.title = java-training
resources.router.routes.viewbatchcourse.reqs.course_id = "\d+"
resources.router.routes.viewreviews.route = "/java-reviews/:course_id/"
resources.router.routes.viewreviews.defaults.controller = course
resources.router.routes.viewreviews.defaults.action = view-reviews
resources.router.routes.viewreviews.defaults.title = java-reviews
resources.router.routes.viewreviews.reqs.course_id = "\d+"
For question 2. As you describe it, simply no.
Re: Q1. I haven’t tested this, but hopefully it is pointing you in the direction you want to go.
resources.router.routes.viewreviews.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.viewreviews.route = "(.+)-reviews/(\d+)"
resources.router.routes.viewreviews.defaults.controller = "course"
resources.router.routes.viewreviews.defaults.action = "view-reviews"
resources.router.routes.viewreviews.map.1 = "title"
resources.router.routes.viewreviews.map.2 = "course_id"
Re: Q2. I think you'd need to either forward the user to another (parameterless) URL or handle this via javascript. See Modify the URL without reloading the page.

Zend Framework Router dynamic routes

I bumped into a problem and I can't seem to find a good solution to make it work. I have to make some dynamic routes into a Zend Framework project. I'll explain shortly what my problem is:
I need to have dynamic custom routes that "extend" the default route (module/controller/action/params). The project I'm working for has several partners and the routes have to work with those.
To store the partners I've made a static class and it looks like this.
<?php
class App_Partner
{
static public $partners = array(
array(
'name' => 'partner1',
'picture' => 'partner1.jpg'
),
array(
'name' => 'partner2',
'picture' => 'partner2.jpg'
),
array(
'name' => 'partner3',
'picture' => 'partner3.jpg'
)
);
static public function routePartners() {
$partners = array();
foreach(self::$partners as $partner) {
array_push($partners, strtolower($partner['name']));
}
$regex = '(' . implode('|', $partners) . ')';
return $regex;
}
}
So App_Partner::routePartners() return me a string like (partner1|partner2|partner3) which I use to create the right routes. My goal is to have the custom routes for each partner for every route I have set in the Bootstrap. So if I have a route add-product.html set I want it to work for each partner as partner1/add-product.html, partner2/add-product.html and partner3/add-product.html.
Also, partner1/, partner2/, partner3 should route to default/index/index.
In fact, I made this thing to work using routes like the one below.
<?php
$routeProposal = new Zend_Controller_Router_Route_Regex(
App_Partner::routePartners() . '?/?proposals.html',
array(
'module' => 'default',
'controller' => 'proposal',
'action' => 'index',
'page' => 1
),
array( 1 => 'partner'),
"%s/proposals.html"
);
$router->addRoute('proposal', $routeProposal);
The problem
The above route works fine if I use a partner in the request URI, but if I don't, I get double slashes like public//proposals.html because of the reverse route set in the route above to be "%s/proposals.html". I can't seem to find a way to avoid this reverse route because I build my urls using the url view helper and if the reverse route isn't set I get an exception stating this.
I also need the routes to work without a partner set, which will be the default way (add-product.html, proposals.html etc).
From your description, it seems like you're looking for a zend router chain, where your partner is an optional chain.
Here's a similar question, but using a hostname route : Zend Framework: get subdomain parameter from route. I adapted it to solve your problem, just put the following in your Bootstrap.php to initialize the routing :
protected function _initRoute()
{
$this->bootstrap('FrontController');
$router = $this->getResource('FrontController')->getRouter();
// Default route
$router->removeDefaultRoutes();
$defaultRoute = new Zend_Controller_Router_Route(
':controller/:action/*',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
)
);
$router->addRoute('default', $defaultRoute);
$partnerRoute = new Zend_Controller_Router_Route(
':partner',
array('partner' => 'none'),
array('partner' => '^(partner1|partner2|partner3)$')
);
$router->addRoute('partner', $partnerRoute->chain($defaultRoute));
}
Change as you see fit. In your controllers you will only get a value for the partner parameter if it was actually specified AND valid (you will get a routing error if the partner doesn't exist)...
I use a similar process to detech lang, in my route (but with a ini file).
You can use a default value for you partners parameter to make the route working without partner, and add a ? to your regex.
But actually, I don't know how to avoid the double //...
Hope that helps.
EDIT: For your information, here is a simplified version of my route with language:
routes.lang.type = "Zend_Controller_Router_Route"
routes.lang.route = "lang/:language/*"
routes.lang.reqs.language = "^(en|fr|nl|de)?$"
routes.lang.defaults.language = none
routes.lang.defaults.module = default
routes.lang.defaults.controller = index
routes.lang.defaults.action = language

How to create a custom router in Zend-Framework?

I am using a custom Router to enable pages like:
mytutorialsite.com/category/:categoryname
# added to application.ini
resources.router.routes.categorynameOnCategory.route = /category/:categoryname
resources.router.routes.categorynameOnCategory.defaults.module = default
resources.router.routes.categorynameOnCategory.defaults.controller = category
resources.router.routes.categorynameOnCategory.defaults.action = categoryname
I also have database table 'categories' in which all categories are stored. For example, let's say the following categories are currently stored in my database:
- html
- css
- js
- php
This means, the following pages exist:
mytutorialsite.com/category/html
mytutorialsite.com/category/css
mytutorialsite.com/category/js
mytutorialsite.com/category/php
But when you visit a page with a categoryname that is not listed in the database, like:
mytutorialsite.com/category/foo
You should get a 404 Page Does Not Exist message.
How do I achieve that?
I think you mean with categoryname as action in your route the :categoryname should be used as an action? There are two methods you can use. First is you add only the routes to the router where categories exist. When category/foo is requested the router won't recognize the route and throw the 404 error.
The second option is you catch all category/* routes and inside your action you check if the category exists.
For the first option, add a frontController plugin with a routeStartup function. In this hook you can do:
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
// Get the router
$router = Zend_Controller_Front::getInstance()->getRouter();
// Fetch all your categories
$category = new Application_Model_Category;
$categories = $category->fetchAll();
// Loop and add all individual categories as routes
foreach ($categories as $category) {
$route = 'category/:' . $category->name;
$params = array(
'module' => 'default',
'controller' => 'category',
'action' => $category->name
);
$route = new Zend_Controller_Router_Route($route, $params);
$router->addRoute($category->name, $route);
}
}
The other method is more simple for the route. In your application.ini:
resources.router.routes.category.route = "category/:action"
resources.router.routes.category.module = "default"
resources.router.routes.category.controller = "category"
Now all requests from category/SOMETHING will go to the default module, category controller. The dispatcher checks if the action SOMETHING exists. When it does, it executes the action. When not, a Zend_Controller_Action_Exception ("action does not exist") is throw.
So in short, both methods work. With the first you get more control. The second is more simple but when e.g. an editAction, addAction or removeAction in the categoryController exist, they can be triggered as well (so be careful with that method).
PS. Of course, the routeStartup function should have a caching mechanism to prevent a database query on each request.

How can I setup a simple custom route using Zend Framework?

I'm looking to setup a custom route which supplies implicit parameter names to a Zend_Application. Essentially, I have an incoming URL which looks like this:
/StandardSystems/Dell/LatitudeE6500
I'd like that to be mapped to the StandardsystemsController, and I'd like that controller to be passed parameters "make" => "Dell" and "model" => "LatitudeE6500".
How can I setup such a system using Zend_Application and Zend_Controller_Router?
EDIT: I didn't explain myself all that clearly I guess -- if make and model aren't present I'd like to redirect the user to another action on the StandardsystemsController. currently, using Ballsacian1's answer with this in application.ini:
resources.router.routes.StandardSystem.route = "/StandardSystem/:make/:model"
resources.router.routes.StandardSystem.defaults.controller = "StandardSystem"
resources.router.routes.StandardSystem.defaults.action = "system"
resources.router.routes.StandardSystem.defaults.make = ""
resources.router.routes.StandardSystem.defaults.model = ""
resources.router.routes.StandardSystemDefault.route = "/StandardSystem"
resources.router.routes.StandardSystemDefault.defaults.controller = "StandardSystem"
resources.router.routes.StandardSystemDefault.defaults.action = "index"
You would first instantiate a new Zend_Controller_Router_Route to create your route.
$stdsys_route = new Zend_Controller_Router_Route(
'/StandardSystems/:make/:model',
array(
'controller' => 'StandardsystemsController',
'action' => 'myaction'
);
);
This route then needs to be added to your router.
$front_controller = Zend_Controller_Front::getInstance();
$front_controller->getRouter()->addRoute('stdsys', $stdsys_route);
Now when you dispatch, the route should take effect.
References:
http://framework.zend.com/manual/en/zend.controller.router.html
Resources:
resources.router.routes.StandardSystems.route = "/StandardSystems/:make/:model"
resources.router.routes.StandardSystems.defaults.controller = "standardsystems"
resources.router.routes.StandardSystems.defaults.action = "index"

How to set up Hierarchical Zend Rest Routes?

With the Zend Framework, I am trying to build routes for a REST api on resources organized in the following pattern:
http://example.org/users/
http://example.org/users/234
http://example.org/users/234/items
http://example.org/users/234/items/34
How do I set up this with Zend_Rest_Route?
Here is how I have setup the route for the users resource (users/:id) in my bootstrap.php file:
$this->bootstrap('frontController');
$frontController = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($frontController);
$frontController->getRouter()->addRoute('default', $restRoute);
[As far as I understand, this is a catch all route so users/324/items/34 would results in parameters set as id=324 and items=34 and everything would be mapped to the Users (front module) Model. From there I guess I could just test for the items parameter and retrieve the item #34 for user #324 on a get request.]<=== I just checked it and it doesn't seems to work like that:
Acessing /users/234/items/43 and
var_dump($this->_getAllParams());
in the get action of the rest controller results in the following output:
array(4) {
["controller"]=> string(5) "users"
["action"]=> string(3) "get"
[2]=> string(5) "items" ["module"]=> string(7) "default"]
}
Somehow both ids got lost...
Anyone?
I open sourced the solution into github and as a composer package. see https://github.com/aporat/Application_Rest_Controller_Route.
I added a new class which extends Zend_Controller_Router_Route and adds abiliy to add custom restful routes, such as
$frontController = Zend_Controller_Front::getInstance();
$frontController->getRouter()->addRoute('users-messages', new Application_Rest_Controller_Route($frontController, 'users/:user_id/messages', ['controller' => 'users-messages']));
AFAIK, there is no feature in Zend_Rest_Route which allows you to do something like this. There is a proposal but not sure when it'll be implemented. You can add this in your Bootstrap to set up this custom route.
$front = $this->getResource('FrontController');
$testRoute = new Zend_Controller_Router_Route(
'users/:user_id/items/:item_id',
array(
'controller' => 'users',
'action' => 'item',
'module' => 'default'
)
);
$front->getRouter()->addRoute('test', $testRoute);
user_id or item_id become available in itemAction in UsersController as parameters:
$user_id = $this->_request->getParam('user_id');
Zend_Rest_Route maps the first parameter after the controller name to 'id' only when there is 1 parameter.
Best solution I've come up with is to subclass Zend_Rest_Route and override the match() function. Copy Zend_Rest_Route's match function, but add the following just before the "Digest URI Params" comment (about 60 lines in).
if($pathElementCount > 1 && $path[0] != 'id') {
$params['id'] = urldecode($path[0]);
array_shift($path);
}
That should give you the functionality you're looking for.

Categories