I have a link that goes like this:
$this->Html->link('Welcome', array('controller' => 'users', 'action' => 'add'));
I suspect it doesn't work (Firefox says the redirect will never complete), so I did this:
$this->Html->link('Welcome', array('controller' => 'users', 'action' => 'add', 'my'=>false));
'my' is a prefix. I also have 'admin', 'ajax', and 'rss'. So now instead of pointing to /users/add or /my/users/add, the link points to /rss/users/add. The link is in a header, in a layout so I don't know which prefix it will be invoked from. I want to go to /users/add, that is, leave all prefixes altogether. How do I do that in CakePHP? I'm using 1.3 version.
Get the prefix from $this->params, like this:
$this->Html->link('Welcome',
array('controller' => 'users',
'action' => 'add',
$this->params['prefix'] => false));
Two things:
First, if Firefox says the redirect will never complete, then you have something wrong within the 'receiving' action (i.e. the add action) or with Routes configuration that is redirecting over and over again.
Second: I trust you configured your custom prefixes following the Cookbook. But I also see that there can be a problem with the order of parameters. Place prefixes at the beginning, like this:
$this->Html->link('Welcome', array('my' => false, 'controller' => 'users', 'action' => 'add'));
I say so because if I recall correctly, parameters after the 'action' parameter are treated as additional url parameters (i.e. /users/add/foo/bar:bla).
In cakephp 3, use something like this below
$this->Html->link('Welcome', array(
'controller' => 'users',
'action' => 'add',
'prefix' => false)
);
Related
I have created URLs in cake 2.x using strings like
$this->Html->link('View', '/posts/view/' . $id)
//posts/view/id
in multiple times And then later decided that /posts should be called /articles instead in URL.
//articles/view/id
I don't want to change my existing code, I want to use Reverse Routing.
I read about the reverse routing but could not find any suitable example
related to my problem.
It will be appreciated if anybody gives me solution regarding my problem ?
Dont hard code the urls, use arrays instead
$this->Html->link('View', array('controller' => 'posts', 'action' => 'view' ,$id));
And in your route file (app/Config/routes.php), specify the routing rule
//The indexaction
Router::connect(
'/articles',
array('controller' => 'posts', 'action' => 'index')
);
//The view action
Router::connect(
'/articles/:id',
array('controller' => 'posts', 'action' => 'view'),
array(
'pass' => array('id'),
'id' => '[0-9]+'
)
);
I am facing some problems in routing under cakephp
there are two actions in my controller
they are as below:
example.com/posts/show/show-by-day
example.com/posts/view/slug-post
I want them as:
example.com/article/show-by-day.html
example.com/article/slug-post.html
So i routes file under config file I wrote as:
Router::connect('/article/:show_by_day', array('controller' => 'posts', 'action' => 'show'), array('pass' => array('show_by_day')));
Router::connect('/article/:slug', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('slug')));
its working fine when I hit the url example.com/article/show-by-day.html
but when I hit url example.com/article/slug-post.html , its again point to action show.
So how can I solve it?
Many thanks!
try this
Router::connect( '/article/show-by-day.html',
array(
'controller' => 'posts',
'action' => 'show'
),
array(
'pass' => array('show_by_day')
)
);
Router::connect( '/article/slug-post.html',
array(
'controller' => 'posts',
'action' => 'view'
),
array(
'pass' => array('slug')
)
);
Explanation:
The first parameter of the Router::connect is the exact URL you want to match - in the original code, a : was included - which parameterized the URL instead of using exact match. The second and third paramters in Router::connect are the actual controller / action that need to be invoked along with required parameters.
I have a controller setup to accept two vars: /clients/view/var1/var2
And I want to show it as /var1/var2
SO i tried
Router::connect('/*', array('admin'=>false, 'controller' => 'clients', 'action' => 'view'));
But this stops all other controllers working as /* routes everything
All other pages that are on the site are within the admin prefix so basically i need a route that is ignored if the current prefix is admin! I tried this (regex is from Regular expression to match a line that doesn't contain a word?):
Router::connect('/:one', array('admin'=>false, 'controller' => 'clients', 'action' => 'view'), array(
'one' => '^((?!admin).*)$'
));
But I think the regex is incorrect because if i naviate to /test it asks for the tests controller, not clients
My only other routes are:
Router::connect('/admin', array('admin'=>true, 'controller' => 'clients', 'action' => 'index'));
Router::connect('/', array('admin'=>false, 'controller' => 'users', 'action' => 'login'));
What am I doing wrong? Thanks.
I misunderstood your question the first time. I tested your code and didn't get the expected result either. The reason might be that the regex parser doesn't support negative lookahead assertion. But I still think you can solve this with reordering the routes:
The CakeBook describes which routes are automatically generated if you use prefix routing. In your case these routes have to be assigned manually before the '/*'-route to catch all admin actions. Here is the code that worked for me:
// the previously defined routes
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/admin', array('controller' => 'clients', 'action' => 'index', 'admin' => true));
// prefix routing default routes with admin prefix
Router::connect("/admin/:controller", array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin/:controller/:action/*", array('prefix' => 'admin', 'admin' => true));
// the 'handle all the rest' route, without regex
Router::connect(
'/*',
array('admin'=>false, 'controller' => 'clients', 'action' => 'view'),
array()
);
Now I get all my admin controller actions with the admin prefix and /test1/test2 gets redirected to the client controller.
I think the solution is described in the bakery article on routing - "Passing parameters to the action" (code not tested):
Router::connect(
'/clients/view/:var1/:var2/*',
array(
'controller' => 'clients',
'action' => 'view'
),
array(
'pass' => array(
'var1',
'var2'
)
)
);
The controller action would look like:
public function view($var1 = null, $var2 = null) {
// do controller stuff
}
Also you have too look at the order of your routes (read section "The order of the routes matters"). In your example the '/*' stops all other routes if it comes first, if you assign the rule after the others it handles only requests which didn't match any other route.
I'm using Zend Framework and the URL View Helper to create URLs
I have some lines like this in my navigation:
$this->url(array('controller' => 'index', 'action' => 'index'))
$this->url(array('controller' => 'who', 'action' => 'view', 'id' => $row->who_id));
$this->url(array('controller' => 'projects', 'action' => 'view', 'id' => $row->mai_id));
$this->url(array('controller' => 'content', 'action' => 'view', 'type' => 'theater', 'id' => $row->the_id));
$this->url(array('controller' => 'shows', 'action' => 'view'));
This way, at first, I have some URL like this
http://ccgss.local/information/location
http://ccgss.local/who/view/id/1
But when I access another link with more parameters like http://ccgss.local/content/view/id/1/type/theater
it messes up with the parameters that still were there: http://ccgss.local/who/view/id/1/type/theater
I mean, the parameters don't clean up when I access another page.
How do I fix this?
You need to reset parameters when calling url helper.
$this->url(array('controller' => 'index', 'action' => 'index'), null, true);
The second argument is the name of the route to use. Keep it null if you want to use the current route.
The third argument tells whether or not to reset parameters. It's false by default.
So, just set it to true to get rid of existing parameters.
I'm generating plain simple links with CakePHP's HtmlHelper the following way:
$html->link("Newest", array(
'controller' => 'posts',
'action' => 'listView',
'page'=> 1,
'sort'=>'Question.created',
'direction'=>'desc',
));
Having the following route rule:
Router::connect('/foobar/*',array(
'controller' => 'posts',
'action' => 'listView'
));
The link is nicely generated as /foobar/page:1/sort:Question.created/direction:desc. Just as I want, it uses my URL prefix instead of controller/action names.
However, for some links I must add named parameters like this:
$html->link("Newest", array(
'controller' => 'posts',
'action' => 'listView',
'page'=> 1,
'sort'=>'Question.created',
'direction'=>'desc',
'namedParameter' => 'namedParameterValue'
));
The link in this case points to /posts/listView/page:1/sort:Question.created/direction:desc/namedParameter:namedParameterValue. But I do not want to have contoller/action names in my URL-s, why is Cake ignoring in this case my routers configuration?
Quite undocumented, but mentioned, this solved it:
Router::connectNamed(array('namedParameter', 'page', 'sort', 'direction'));