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
Related
I obviously have a fundamental misunderstanding of how pagination works in CakePHP.
I have the following route set up which shows all posts in a category:
Router::connect('/:parent/:category',
array('controller' => 'posts', 'action' => 'viewCategory'),
array('parent' => '[a-z0-9-]+', 'category' => '[a-z0-9-]+'));
The pages work fine, however the pagination helper is outputting the wrong links for pagination.
I'm using $this->Paginator->numbers().
It's outputting links in this format: mysite.com/posts/viewCategory?page=2
rather than like this: mysite.com/parent-category/sub-category?page=2.
I've tried adding the following route after the first one and it still doesn't work:
Router::connect('/:parent/:category/:page',
array('controller' => 'posts', 'action' => 'viewCategory'),
array('parent' => '[a-z0-9-]+',
'category' => '[a-z0-9-]+',
'page' => '[0-9]+'));
For reference, my pagination options set in my view are as so:
<?php $this->Paginator->options(
array('url' =>
array('controller' => 'posts', 'action' => 'viewCategory')
)); ?>
What am I doing wrong here?
You are setting the url yourself
This is your paginator options call:
<?php
$this->Paginator->options(array(
'url' => array(
'controller' => 'posts',
'action' => 'viewCategory'
)
));
?>
Where you are overriding the current url - and explicitly requesting that the paginator uses the the '/posts/viewCategory' url (with no arguments) as it's base url.
Just don't define the url
Simply don't call options and the helper will use the current url - that should mean that if the current url is:
/parent-category/sub-category
Then page 2 will be (assuming you are using the paramType option to use GET arguments rather than named parameters):
/parent-category/sub-category?page=2
If that's not the case there's information missing from the question; it's important to distinguish between "vanity routes not being used" and "the url is not equivalent (the current situation).
Just had a battle fixing something similar and came across this post. Though old, but I think my answer might save someone the time I had to spend fixing it.
Basically, what you need to do is call the Paginator->options() before Paginator->numbers(), thus:
$this->Paginator->options(
array(
'controller' => 'parent-category',
'action' => 'sub-category'
)
);
Though the controller and action do not exist, it just tricks CakePHP to use them "AS IS", since the reverse routing isn't working!
And for those (like me), who want have set up a route similar to
Router::connect(
'/go/page:id',
array(
'controller' => 'blog',
'action' => 'paginated'
)
);
There might be difficulty setting up the Paginator options. This, however, worked for me:
$this->Paginator->options(
array(
'controller' => 'go',
'action' => '/'
)
);
I guess you know why it worked ;)
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.
Having a bit of trouble getting my URLs to work properly.
The URL looks like this: /messages/from/1/page/5
My route looks like this
$router->addRoute('messages-from',
new Zend_Controller_Router_Route('messages/from/:user_id/:page', array(
'controller' => 'messages',
'action' => 'from',
'page' => 1
))
);
Which works fine. But the URL is missing the /page/ part. If I add it in:
'messages/from/:user_id/page/:page'
then it breaks and the user_id param is always null.
How can I fix this?
Thanks!
Since you want to be able to leave off the /page/ part from the URL, you would have to define two separate routes, one that matches the user ID and page parameters and one that only matches the user ID without the page so the router can find route matches in both cases.
Alternatively, this regex based route works in both cases.
$route = new Zend_Controller_Router_Route_Regex(
'messages/from/(\d+)(?:/page/(\d+)/?)?',
array(
'controller' => 'messages',
'action' => 'from',
'page' => 1,
),
array(
1 => 'from',
2 => 'page',
)
);
$router->addRoute('messages-from', $route);
Based on the URL you supplied, I assumed in the regex that the from parameter is an integer. If you can have strings passed, you will need to change the (\d+) pattern to something more suitable like ([\w\d_-\.]+).
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');
...
}
I've set the following routes for in Zf:
$router->addRoute(
'page',
new Zend_Controller_Router_Route('stranka/:niceuri/:id', array('controller' => 'page', 'action' => 'index'))
);
$router->addRoute(
'cat',
new Zend_Controller_Router_Route('kategoria/:niceuri/:id', array('controller' => 'category', 'action' => 'index'))
);
The problem is that the 'cat' route keeps overwriting the other 'page' route and simle $this->url() routes aswell. That means, that any links using the 'page' route and having the param 'niceuri' defined have the the value of 'niceuri' equal to the currently open page using the 'cat' route - which they sholdn't have. (sorry, does that make sense to you?) Any ideas on how to solve this behavior? Thanks a lot.
I didn't exactly understand what did you mean, but...
When you calling $this->uri helper in view you can set the name of the preffered router to use to assemble the url. Something like this:
echo $this->uri(array('niceuri' => 'Ololo', 'id' => '123'), 'page');
Hope this helps.