Zend url view helper - keeping existing params in a reversed route - php

At the moment in my ZF project have a URL structure like this:
/news/index/news_page/1/blog_page/2
When I generate my pagination I use the URL helper as follows:
<?php echo $this->url(array('blog_page'=>3)); ?>
Which generates a URL like this:
/news/index/news_page/1/blog_page/3
What I'd like to do is use a custom route to have nicer URLs, something like this:
new Zend_Controller_Router_Route(
'news/:news_page/:blog_page',
array('controller' => 'news', 'action' => 'index')
);
However, when I try an use this route in the view helper:
<?php echo $this->url(array('blog_page'=>3), 'newsIndex'); ?>
It throws an error because I've not specified news_page in the params.
How can I get around this, and tell the url helper to use the 'current' values for these params?

The url helper will use the an existing parameter if it exists in the current request. It seems as if, in your particular case, the news_page param is not set in the request object. Setting a default value for the news_page parameter in your route should solve your problem.
So, your route definition should look something like this:
new Zend_Controller_Router_Route(
'news/:news_page/:blog_page',
array('controller' => 'news', 'action' => 'index', 'news_page' => 1)
);

Related

Getting parameters from URL in Zend

In a Zend application with an example url like this one:
http://example.com/profile/423423(some id)
How do I get param from url?
I try to use:
$this->getRequest()->getParam('action');
But I get action does not exist.
then I try something like this:
protected function _initRouter()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('profile/:catid', new Zend_Controller_Router_Route('profile/:catid',
array(
'module' => 'default',
'controller' => 'profile',
'action' => 'index' // Check your action and controller
)));
}
How do I get param from url?.
You can use the getParam() methods to retrieve request variables like route, GET and POST.
Inside your controllers action you can do this.
$this->getParam('parameterName'); or $this->getRequest()->getParam('parameterName');
If you want to get the name of action and controller that matched the current route you need to use following methods.
$this->getRequest()->getActionName() and $this->getRequest()->getControllerName()
You should just use
$this->getRequest()->getParam('profile');
from your controller.
You shouldn't need to use a custom route.
Kind regards
Garry
EDIT
I have just noticed that you wish to use the profile controller. The simplest solution would be to add an id into your url like.
http://example.com/profile/id/423423
and get the profile id with
$this->getRequest()->getParam('id');

Changing the routes for named parameters in CakePHP

I have the following two routes which make the url /posts/recent show page 1 of the recent filter on index method of my posts controller and also allow paging like: /posts/recent/page:2 by using the * on the next route. As you can see I call page 1 on the first route so that I don't get duplicate urls for page 1.
Router::connect('/posts/recent', array('controller'=>'posts','action'=>'index','filter'=>'recent', 'page' => 1), array('pass'=>array('filter')));
Router::connect('/posts/recent/*', array(
'controller' => 'posts', 'action' => 'index', 'filter'=>'recent'), array(
'named' =>array('page' => '[\d]+'),
'pass'=>array('filter')
)
);
However I would like to make it so that named params do this instead:
/posts/recent/page/2 but how do I do it?
I've looked around the docs but don't seem to see anything about doing this...
Also is it possible to turn off named parameters in favour of query strings?
I'm using CakePHP 2.1 if it matters.
Perhaps with Router::connectNamed()?
http://book.cakephp.org/2.0/en/development/routing.html

Remote controller name from CakePHP URL

I have a problem with the url rewriting.
The problem that I am facing is that currently our urls are like this:
http://www.xyz.com/sc_users/index
I dont want the controller name to be shown in that url.
Is there a way to achieve that??
First of all thank your guys..
Like I have 8 controllers I dont want the controller name to be shown in my url....this is what I want..
To be more precise no controller name in my url
You can define custom routes in app/config/routes.php. You'll find the all about routes in the CakePHP cookbook under Defining Routes. For example, a custom route can look like this:
Router::connect(
'/the_url_you_want_to_use/*', array('controller' => 'sc_users', 'action' => 'index')
);
You need to read up about CakePHP routing, look at the examples under 'defining routes'. Update your question with what you would actually like your URLs to look like and we will be able to help you more effectively.
That's simple :
there is a file called routes.php in /config directory :
You can do url rewriting stuff there like this :
Router::connect('/pages/*', array('controller' => 'cmsPage', 'action' => 'render'));
You can pass more complicated variables to your controller :
Router::connect('/:id-:lang-:profile-:firstName-:lastName-:profile.htm',
array('controller' => 'profiles','action' => 'view'),
array('id'=>'[0-9]*', 'lang'=>'fr','firstName'=>'[^-]*','lastNAme'=>'[^-]*','profile' => $util->keywords['profiles'][0]['fr'], 'pass' => array('id', 'lang'),'profile' => $util->keywords2['profiles'][0]['en'])
)
;
As you can see in the last example I have passed 2 parameters to the controller through 'pass' => array('id', 'lang')

Zend_Router omitting param-key

I've got a question considering Zend_Controller_Router. I'm using a a modular-structure in my application. The application is built upon Zend-Framework. The normal Routes are like this:
/modulename/actionname/
Since I always use an IndexController within my modules, it's not necessary to provide it in the url. Now I am able to append params like this:
/modulename/actionname/paramkey/paramvalue/paramkey/paramvalue
So this is normal in ZF, I guess. But in some cases I don't want to provide a paramkey within the url. For example I want a blog-title to be shown within the url. Of course this is intended for SEO:
/blog/show/id/6/this-is-the-blog-title
In this case, blog is the module, show is the action. id is a paramkey and 6 is the id of the blogpost I want to show. this-is-the-blog-title is of course the headline of the blogpost with the id 6. The problem is, that if I do use the assemble()-method of the router like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html'));
the url results in:
blog/show/id/6/0/this-is-the-blog-title.html
As you can see a 0 is inserted as a key. But I want this 0 to be omitted. I tried this by using the blogtitle as key, like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html' => ''));
This results in:
blog/show/id/6/this-is-the-blog-title.html/
Now the 0 is omitted, but I've got the slash at the end.
Do you have any solution to get an url without 0 as key and without an ending slash?
Regards,
Alex
You might want to use a custom route for this:
$router->addRoute(
'blogentry',
new Zend_Controller_Router_Route('blog/show/:id/:title',
array('controller' => 'index', 'module' => 'blog'
'action' => 'info'))
);
And call your assemble with the route as second parameter. See the Zend_Controller_Router_Route section of the documentation for more details (they even provide examples with assemble).
Or in a more general way:
$router->addRoute(
'generalseo',
new Zend_Controller_Router_Route(':module/:action/:id/:title',
array('controller' => 'index'))
);

Zend - outputting better module url?

I am currently outputting the url of a particular module like so:
$this->view->url(array('controller'=>'index','action'=>'index','module'=>'somemodule'))
The problem is when I view:
/somemodule/action1/paramid/paramval
The url is showing as:
/somemodule/index/index/paramid/paramval
When all I really want is:
/somemodule/
Any ideas? I have used the above url array because I am trying to force it to display the correct url. sadly, simply array('module'=>'somemodule') doesn't work...
Weird, I would've thought it defaults to index/index if they aren't specified. You could try setting up something with Zend_Controller_Router_Route;
$router = $ctrl->getRouter(); // returns a rewrite router by default
$router->addRoute(
'user',
new Zend_Controller_Router_Route('somemodule',
array('module' => 'somemodule',
'controller' => 'index',
'action' => 'index'))
);
You will however need to set up routes to capture the :id/:val into variables. You can do this with another addRoute(), naming each variable prepending a colon.
$router->addRoute(
'user',
new Zend_Controller_Router_Route('somemodule/:id/:val',
array('module' => 'somemodule',
'controller' => 'index',
'action' => 'index'))
);
Then you should be able to view the module without the index/index in the URL which might fix the problem with the url helper.
More info here http://framework.zend.com/manual/en/zend.controller.router.html

Categories