Kohana have a smart routing system, I like feature when I can get url by route(Route::get('route_name',params), How implement this feature to codeigniter? Result that I need http://site.com/Sunglasses/Novus/202/ss14.05.html
Thanks
CodeIgniter doesn't support this feature (some would call it "reverse routing").
Maybe I'm not understanding the question properly as I haven't used Kohona, but if you just want to get the uri segment, you would use:
$this->uri->segment(3); //=202 in http://site.com/Sunglasses/Novus/202/ss14.05.html
To route a url with params you would use something like the following:
$route['products/(:any)/(:num)'] = "products/get_product/$1/$2";
so for example with this you could have a url of site.com/products/sunglasses/202 routing to the get_product() method of your products controller and then you would pick up the uri segment as above.
Related
Is it possible to call a controller and action directly by using query parameters in Laravel? I see some frameworks allow /index.php?_controller=X&_action=Y, or Yii allows /index.php?r=X/Y. I was wondering if something similar was possible in Laravel/Symfony.
symfony
The query string of a URL is not considered when matching routes. In this example, URLs like /blog?foo=bar and /blog?foo=bar&bar=foo will also match the blog_list route.
https://symfony.com/doc/6.0/routing.html
laravel afaik doesnt support that either
you are obviously free to just forward yourself
e.g. write one router that forwards to other controllers
symfony https://symfony.com/doc/6.1/controller/forwarding.html
I use $.getJSON() to retrieve some data for a couple of cascading dropdowns in my form. $.getJSON() automatically appends the parameter at the end of the URL like domain.com/controller/method/?parent=5
So, I've declared my method like public function method($parent) which works file, but the same method will be used from other parts of the website that will call it like domain.com/controller/method/5
I tried to create a route in routes.php like the one below:
$route['business/regions/?parent=(:num)'] = 'business/regions/$1';
but it doesn't seem to work. Am I doing something wrong? Maybe ? is confusing the regex parser of the router? Do I have to escape it somehow to make it a 'literal' ? ?
Or is it that router is not used to 'rewrite' get parameters at all? I'm very confused, as it should work but it doesn't and I'm wondering what's wrong with it...
Codeigniter route parameters are for url parameters. It is particularly useful when trying to create a REST styled url pattern.
What you're trying to do is get url query string from the url which is not supported via the Codeigniter router. For you to get what you want you can do the following:
In your routes.php:
$route['business/regions'] = 'business/regions';
and in your controller Business.php:
public function regions() {
//the numeric id you're looking for
$parent = $this->input->get('parent');
}
I am looking for a way to dynamically route my Laravel 5 application. This is what I want:
http://domain.ext/hello/world
The above url should automatically call the controller 'hello' with the method 'world()'.
Or another example:
http://domain.ext/pages/profile/settings
This URL should rout to the controller pages/profile with the method settings().
Of course, when parameters should be given, I will have to specify the route. But for the more simpler routes I would love to use the example above.
For the people that are familiar with the Kohana Routing style, that is exactly what I am looking for. I have found the following article, it gets me halfway there. Just wondering if I can get the method from the url too somehow.
http://tech.insideroy.com/?p=33
I got stack on this in Codeigniter route, What do I want is to remove the function_name in url to be able to have a short url.
Here is the example want to have in my url
http://mysite.com/controller_name/function_name/id
to this
http://mysite.com/controller_name/id
Is there any other solution to have this if cannot be done in route? thanks!
If the above didn't work you could try:
$route['controller_name/(:num)'] = 'controller_name/lookup_function/$1';
Not much different from what was already suggested other than a hard coded controller name.
You can probably do it by trying:
$route['([a-z]+)/(\d+)'] = "$1/method/$2";
That is if you don't need to change the name of your method.
I'm very new to MVC and I'm trying to get a new site set up using it. For SEO reasons we need to make the url of a page something like "Recruiter/4359/John_Smith" or basically {controller}/{id}/{name}. I have that working when I create the url in the code behind like so...
//r is a recruiter object that is part of the results for the view
r.Summary = searchResult.Summary + "... <a href=\"/Recruiter/" + r.Id + "/" + r.FirstName + "_" + r.LastName + "\">Read More</a>"
But when I am using the collection of results from a search in my view and iterating through them I am trying to create another link to the same page doing something like <%=Html.ActionLink<RecruiterController>(x => x.Detail((int)r.Id), r.RecruiterName)%> but that doesn't work. When I use that code in the view it gives me a url in the form of /Recruiter/Detail/4359 I was told by a coworker that I should use the Html.ActionLink to create the link in both the view and the controller so that if the route changes in the future it will automatically work. Unfortunately he wasn't sure how to do that in this case. So, my problems are...
How can I make the Html.ActionLink work in the view to create a url like I need (like r.Summary above)?
How do I use the Html.ActionLink in a controller instead of hardcoding the link like I have above?
I came across this blog post which got me going in the right direction.
http://www.chadmoran.com/blog/2009/4/23/optimizing-url-generation-in-aspnet-mvc-part-2.html
It is a good idea to use the ActionLink method to write out links as your coworker says, that way they will always match your routes.
In your current case the reason it is writing out the method is because it is based on the default routing. You can fix this by adding another route above the default one in the Global.asax. You just need to stipulate the format you want like this:
routes.MapRoute(
"Recruiter",
"Recruiter/{id}/{name}",
new { controller = "Recruiter", action = "Details" }
);
MVC will work through your routes in the order they are registered so putting this before the default will make it use your route instead.
EDIT:
You might find this route debugging tool useful.