Laravel routing: the Kohana way (routes relative to controller) - laravel

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

Related

Calling a controller/action through query parameters

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

Product And Category Separation In Route (Laravel)

I'm setting up a new route system.
Route::get('/{cat1Url}', 'CategoryController#showCat1')->name('showCat1');
Route::get('/{productUrl}', 'ProductController#showProduct')->name('showProduct');
My sef link is after "/"
But,
{{ route('showProduct',[$p->pr_url]) }}
This method not working with route name. Working only upside route.
I don't want use
"/cat/myVariable"
or
"/product/myVariable"
Can't I use route name to work this way?
What is the solution to this?
In this way, if you make a get request to /something the laravel you start from top of web.php file looking to a route that follows the pattern. Your both routes will follow that pattern, but the laravel will always, pass the first one to controller.
You have two options:
Put only one route, and inside the controller you switch to the appropriate function. But this isn't a great ideia, because this is the function of the Web.php.
Use the routes like the documentation recommend:
Route::get('/cat/{catId}', 'CategoryController#showCat')->name('showCat');
Route::get('/prod/{productId}', 'ProductController#showProduct')->name('showProduct');
and in Controller you make the appropriate handler of your Category or Product.
You will have to have a way to tell Laravel which url to be mapped to what otherwise it will always use the last defined route. So in your case calling /myVariable and /myVariable it will use the latest definition which is showProduct. The only other way is if you use regular expression to differentiate the variables. For example:
Route::get('/{cat1Url}', 'CategoryController#showCat1')
->name('showCat1')->where('cat1Url', 'cat-*');
Route::get('/{productUrl}', 'ProductController#showProduct')
->name('showProduct')->where('productUrl', 'prod-*');
This way your slugs need to start with what you define, but you cannot use just id as a numeric value for both.

Route::get function like Kohana in CodeIgniter

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.

CodeIgniter, Multiple url possibilities that direct to same method

I just started learning codeigniter, and i noticed that. After writing my own routes like this
$route['index'] = "front/index";
$route['page/(:any)'] = "front/page/$1";
$route['section/(:any)'] = "front/section/$1";
Now i can visit the methods and controllers using the old routes and the new ones.
URLS now possible:
localhost/index
localhost/front/index
localhost/front/index.html
Too many urls directing to the same location, i was wondering if it were possible to have only 1 url per each method, restricting all others without using external code. From code igniter itself.
Also: this destroys my ability to use the uri class to get segments from the url.
The easiest way is to just have your index page as your default controller's only (index) function, In your case just the
$route['default_controller'] = "front";
Then all other pages have other controllers with their corresponding URI names, then you dont need ANY routing except the default. I ususally end up with a handful controllers like page.php, news.php, blog.php, products.php, admin.php etcetera.

MVC Routing Engine routes same formatted route to different controller actions

Okay, I did my homework and search SO, and indeed I found similar questions but not reporting the behavior I'm getting.
Here is the deal, I have defined a route:
routes.MapRoute("CategoryName", "Category/Name/{text}",
new { controller = "Category", action = "Name", text = "" });
The twist here is the following:
This url: http://www.url.com/Category/Name/existingCategoryName
And this url: http://www.url.com/Category/Name/anotherExistingCategoryName
Both url's should go to the same controller method which is public ActionResult Name(string text) but sadly the first url is going to the default Index method, the second is being routed correctly.
I wonder why this happens, as I've been with .net mvc for several years and never experienced this behavior.
As a side note here are some facts:
As it's being route to different methods, I doubt the code inside them has something to do with it.
When manually write the category to something it doesn't exists in the DB as a category name it goes through the Name method.
The routes are placed correctly, as I'm aware the first route that matches the pattern will win.
Even I tried place the CategoryName route first, the behavior is the same.
When writing each link in the Category/Index I use the same #Html.RouteLink() helper, so all the links are formatted the same way.
Thanks in advance!
Are you using the - sign in the failing route?
Maybe you can find more information with the Routing debugger
And maybe you can look at this question: Failing ASP.NET MVC route. Is this a bug or corner case?
Phil Haack also give an possible answer to your problem in: ASP.NET routing: Literal sub-segment between tokens, and route values with a character from the literal sub-segment

Resources