In a Zend application with an example url like this one:
http://example.com/Controller/action/42
Is there any convenient way to retreive that last parameter? (The "42")
$this->_request->getParams();
won't work since it only retreives name value pairs.
It looks like you're looking for Zend_Controller_Router.
Zend_Controller_Router_Route is the standard framework route. It combines ease of use with flexible route definition. Each route consists primarily of URL mapping (of static and dynamic parts (variables)) and may be initialized with defaults as well as with variable requirements.
$route = new Zend_Controller_Router_Route(
':controller/:action/:id',
array(
'controller' => 'index',
'action' => 'index',
'id' => 0
),
array('id' => '\d+') // Makes sure :id is an int
);
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('myRouteName', $route);
its not valid zf url
url is usually consist of
/controller/action/id/value/id2/value2/....../idN/valueN
and then can read all of these params together :
$this->_getAllParams()
$this->_request->getParams();
or by its ID using your own
$this->_request->getParam("id");
$this->_getParam("id")
First of all, as #adlawson, state you need to create a route that accept the parameter. By doing this you also give a name to this parameter. The code #adlawson proposed is good enough:
$route = new Zend_Controller_Router_Route(
':controller/:action/:id',
array(
'controller' => 'index',
'action' => 'index',
'id' => 0
),
array('id' => '\d+') // Makes sure :id is an int
);
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('myRouteName', $route);
Then, the simplest way, in the controller, to retrieve the value of the id from your url http://example.com/Controller/action/42 is the following :
public function indexAction () {
...
$id = $this->params()->fromRoute('id');
...
}
Related
I'm currently creating a new version of my website using Zend Framework and I'm stuck with a little problem I've seen in the past.
There are my routes: (a part)
// BLOG -> CATEGORIES
$route = new Zend_Controller_Router_Route(
'blog/categories',
array(
'module' => 'blog',
'controller' => 'categories',
'action' => 'index'
)
);
$router->addRoute('blog-categories', $route);
// BLOG -> CATEGORIES -> LIST ARTICLES (:alias = name of the category)
$route = new Zend_Controller_Router_Route(
'blog/categories/:alias',
array(
'module' => 'blog',
'controller' => 'categories',
'action' => 'list',
'alias' => null
)
);
$router->addRoute('blog-categories-list', $route);
The problem is that: when I go to /blog/categories/, it brings me the list action. What I don't want. I need the index.
Is there a way to fix that without using, for exemple, /blog/categories/view/:alias ?
Note: I have the same problem for /blog/ (list all articles) and /blog/:alias/ (display single article).
By including 'alias' => null you're specifying a default value for the :alias parameter, used if it is not in the URL. This is why your second route is always matching. Remove this and it should work as you are wanting it to.
I'm using Zend Framework 1.12 and have this route:
$router->addRoute('item_start',
new Zend_Controller_Router_Route_Regex(
'(foo|bar|baz)',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
1 => 'area'
),
'%s'
)
);
Problem is, when I call '/foo' and use the Url Helper in the View, it doesn't give me any parameters:
$this->url(array("page"=>1));
// returns '/foo' (expected '/foo/page/1')
$this->url(array("page"=>1), "item_start", true);
// also returns '/foo'
Any idea how to get the page-parameter into the URL? I can't use the wildcard like in the standard route, can't I?
In addition to David's suggestions, you could change this route to use the standard route class, and then keep the wildcard option:
$router->addRoute('item_start',
new Zend_Controller_Router_Route(
':area/*',
array(
'module' => 'default',
'controller' => 'item',
'action' => 'start'
),
array(
'area' => '(foo|bar|baz)'
)
)
);
// in your view:
echo $this->url(array('area' => 'foo', 'page' => 1), 'item_start');
Your Regex route doesn't have a page parameter, so when the url view-helper ends up calling Route::assemble() with the parameters you feed it, it ignores your page value.
The two choices that come to mind are:
Modify your regex to include a (probably optional with default value) page parameter
Manage the page parameter outside of your route in the query string.
I have one route that looks like this:
Router::connect('/Album/:slug/:id',array('controller' => 'albums', 'action' => 'photo'),array('pass' => array('slug','id'),'id' => '[0-9]+'));
and another like this:
Router::connect('/Album/:slug/*',array('controller' => 'albums','action' => 'contents'),array('pass' => array('slug')));
for what doesn't match the first. In the 'contents' action of the 'albums' controller, I take care of pagination myself - meaning I retrieve the named parameter 'page'.
A URL for the second route would look like this:
http://somesite.com/Album/foo-bar/page:2
The Above URL indeed works, but when I try to use the HTML Helper (url,link) to output a url like this, it appends the controller and action to the beginning, like this:
http://somesite.com/albums/contents/Album/foo-bar/page:2
Which i don't like.
The code that uses the HtmlHelper is as such:
$html->url(array('/Album/' . $album['Album']['slug'] . '/page:' . $next))
See below url it is very help full to you
http://book.cakephp.org/2.0/en/development/routing.html
Or read it
Passing parameters to action
When connecting routes using Route elements you may want to have routed elements be passed arguments instead. By using the 3rd argument of Router::connect() you can define which route elements should also be made available as passed arguments:
<?php
// SomeController.php
public function view($articleId = null, $slug = null) {
// some code here...
}
// routes.php
Router::connect(
'/blog/:id-:slug', // E.g. /blog/3-CakePHP_Rocks
array('controller' => 'blog', 'action' => 'view'),
array(
// order matters since this will simply map ":id" to $articleId in your action
'pass' => array('id', 'slug'),
'id' => '[0-9]+'
)
);
And now, thanks to the reverse routing capabilities, you can pass in the url array like below and Cake will know how to form the URL as defined in the routes:
// view.ctp
// this will return a link to /blog/3-CakePHP_Rocks
<?php
echo $this->Html->link('CakePHP Rocks', array(
'controller' => 'blog',
'action' => 'view',
'id' => 3,
'slug' => 'CakePHP_Rocks'
));
I'm using a Hostname route to capture a subdomain and use as a category. I then chain a Router route for the controller, action and key/value pairs.
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':customer.ddc.:domain',
array(
'customer' => ':customer'
)
);
$routerRoute = new Zend_Controller_Router_Route(
':controller/:action/*',
array(
'controller' => 'index',
'action' => 'index'
)
);
$chainedRoute = $hostnameRoute->chain($routerRoute);
$frontController->getRouter()->addRoute('default',$chainedRoute);
I can capture everything except the key/value pairs on the URI. Adding them causes the Params object in the Request to not get populated.
This works: http://category.mydomain.com/controller/action/
This does not: http://category.mydomain.com/controller/action/username/frank
Thanks for any suggestions.
Try to use without /*.
$routerRoute = new Zend_Controller_Router_Route(
':controller/:action',
array(
'controller' => 'index',
'action' => 'index'
)
);
as in 12.5.2. Using a Router is described.
The suggested patch didn't work for me. I adapted another patch found elsewhere on the ZF website and it seems to work well: http://pastie.org/1815135
There is indeed a bug which prevents wildcard matching when chaining routes. The comments in the bug description were very helpful in solving this issue with just a few lines of code change.
framework.zend.com/issues/browse/ZF-6654
I am using .ini file to add routes in my application.
resources.router.routes.username.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.username.route = ":username.example.com"
resources.router.routes.username.defaults.module = "userinfo"
resources.router.routes.username.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.username.chains.index.route = ":language/:controller/:action/*"
resources.router.routes.username.chains.index.defaults.controller = "index"
resources.router.routes.username.chains.index.defaults.action = "index"
1) http://john.example.com/fr/controller/action
2) http://john.example.com/fr/controller/action/id/10
#1 url it's work. Request parameter here
Request Parameters:
array (
'language' => 'fr',
'controller' => 'controller',
'action' => 'action',
'username' => 'john',
'module' => 'userinfo',
)
#2 url it's not work. Request parameter here
Request Parameters:
array (
'controller' => 'fr',
'action' => 'controller',
'module' => 'default',
)
Can anyone suggest a solution for this.
did you set a default language for the route? by saying it doesn't work could you provide more details?
I ran into some similar problem with Zend_navigation not taking in the language param and always switch to the default lang that i defined.
Eventually i have to hack it by manually setting default language in the routshutdown in a custom plugin please see my post if it helps
Zend Framework: Zend_translate and routing related issue
resources.router.routes.username.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.username.route = ":username.example.com"
resources.router.routes.username.defaults.module = "userinfo"
resources.router.routes.username.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.username.chains.index.route = ":language/:controller/:action/*"
resources.router.routes.username.chains.index.defaults.controller = "index"
resources.router.routes.username.chains.index.defaults.action = "index"
resources.router.routes.username.chains.index.defaults.language = "en"
I add to default language parameter in route. But this url not work
http://john.example.com/fr/controller/action/id/10
Request Parameters:
array (
'controller' => 'fr',
'action' => 'controller',
'module' => 'default',
)
Looks like the route has been reset to default. I run into that before
create a plugin and register to the front controller. Define dispatchLoopStartup() in there try set the language parameter or module name using
$router = $fc->getRouter();
$router->setGlobalParam('lang','somelanguage');
$router->setGlobalParam('module','somemodule');
see if you can bring back the correct route.