I want to set the cakephp route for URL like this: /param1/contorllername/actionname/ How can I do that, and if I do it, how do I use $this->redirect(array('controller'=>$controllername,'action'=>$actionname)) to redirect my page. Similarly how do I use $this->Html->link for this url.
you can define this in your routes.php
Router::connect(
'/:param1/:controller/:action',
array(),
array('pass' => array('param1'))
);
Then you can declare route like this
$this->redirect(array('controller'=>'posts','action'=>'view','param1'=>10))
This will redirect to /10/posts/view
Related
I want to create a routing in Kohana Framework Version 3.3.1.
I want URL like http://www.test.com/male/London and internally they will act like below URL-
http://www.test.com/list/search/London
I want to hide the controller and action name from the URL.
Any help is greatly appreciated.
This can be accomplished by using routes in bootstrap.php and/or a module's init.php file.
For example, you could set up a route for (male/<location>) and then your default controller would be list and the action search.
You could then access the location within the controller/action by using $this->request->param('location'); to be used in whatever DB query you need it.
Confused? Have a read through this section of Kohana Docs and it should all make sense.
You have to do two changes in your file:
Bootstarp file :
Route::set('list', 'male/<id>' )
->defaults(array(
'controller' => 'list',
'action' => 'search',
));`
And second is you can make your link like
href="<?php echo URL::site('male/'.id, TRUE) ?>">
And this route file should above at your default route file.
So, I would really like to create "landing page" routes for each of a particular type of entity.
So, let's suppose I have a site that is about comic heroes.
I would like landing pages like http://myherosite.com/superman and http://myherosite.com/batman, etc.
I know how to accomplish this with something like http://myherosite.com/heroes/superman and http://mysite.com/heroes/batman. The "heroes" in the URL allow for a specific route and thus controller and default action.
Is it possible to setup a route that will accomplish this and still leave the default route ("{controller}/{action}/{id}") in place (I am using that).
Thanks
You could add a route before "Default" that interprets all single-segment paths as containing an action method of the heroes controller.
routes.MapRoute(
name: "Landings",
url: "{action}",
defaults: new { controller = "Heroes" }
);
Of course, that would prevent you from using any default action for the default route because those are single segments paths as well.
I'm trying to route URLs like stats/non-existent-page to a default 'not found' page, where I'd like to display a 'Did you mean' suggestion along with a not found message, how can I route all non-existent URLs to a default controller/action?
Something like:
match 'stats/*path' => 'default#non_existent'
Add this after your other 'stats/..' urls , This will route your all stats/* paths to the controller action mentioned with path variable you can use to determine your 'did you mean' suggestion..
I define a lot of explicit routes. One of them is:
routes.MapRoute("default", "",
new { controller = "Home", action = "Index" });
At the end, I define a catchall route:
routes.MapRoute("PageNotFound", "{*url}",
new { controller = "Error", action = "Http404" });
If I go to the homepage http://localhost, then the http404 page is shown. And strangely, if I remove the catchall route, then the welcome page appears correctly.
Note also that I have a menu where I call Url.RouteUrl("default") and the link to the homepage is correctly generated.
So, why is my default route not activated when the catchall route exists?
Update: I'm using routes.RouteExistingFiles=true. If I remove it, then it works as expected. But I need it to be set to true. What's the problem here?
Thanks.
If you use "routes.RouteExistingFiles=true" it means it will route existing (physically exist) files as its own - so routing will be skipped for those. I think in your root website there is probably a "default.aspx" or "index.htm" or something like that.
Turning on RouteExistingFiles will then allow those files to be executed normally (instead of via routing).
Now I think what happen is that your catchall routing is overriding you RouteExistingFiles - so it automatically routes the default.aspx into your 404 catchall.
If you still have the default route (I.E. {controller}/{action}/{id}) in RegisterRoutes() it will trap all URLs that match the format of a normal MVC request.
In other words the catch-all route can only intercept a bad URL if it doesn't fit the normal format (blah/blah/blah/blah).
In the case of a non-existent controller the exception must be handled through conventional ASP.NET handling.
Theres a good description of handling this here
Did you try to put a constraint on the catch all route? Constraint should tell it that the catch-all segment should not have 0 characters.
I have actions that take string id parameters that are based on a username which can include characters that require encoding, for instance "user?1"
If I use ActionLink() to generate the links, passing the string without encoding, it generates a link like this: http:\\localhost\controller\action\user?1, and the action gets passed "user" as the id.
If I UrlEncode() the string before passing it to ActionLink, then the link generated is: http:\\localhost\controller\action\user%253f1 as ActionLink will then encode the '%' character for you. Besides this looking ugly, it then also generates a HTTP Error 400 - Bad Request when following the link which I've not yet tracked down the cause of.
Is there any way that I can generate the url like: http:\\localhost\controller\action\user%3f1?
How about removing the ? character or replacing it with something else like a dash (-) or underscore (_) ?
You should look in the Global.asax.cs file
add another route for your convenience, in this case, the ff. might work:
routes.MapRoute(
null,
"{controller}/{action}/user/{id}",
new { controller = "Home", action = "Index" }
);
I guess this is what you want, to separate action for each users, but i suggest you use cookie for this purpose.
PS: Remember to put that one on top of your default route since routing is trying to match from top to bottom.
you can create a jquery plugin that check all the links and replace the char that you need to replace with the new value.
and after apply this plugin to all the ActionLinks