Laravel 4.2: Routig controller with alias? - laravel

I want to Route to my controller with alias but for readability, and less code I'd like to use Route::controller('/login','LoginController'). Is there a way to use both? Or is there any other way to reference a method in my controller if I use this Route::controller syntax?

I solved this rather easily. You can use the syntax Route::controller(...) and instead of using route('route_name') to get the route by alias use action(Controller#method). Although it would be still nice to have aliases.

just use resource route it will automatically create these routes:
index
create
show
edit
update
destroy
add this to routes.php
Route::resource('someroute', 'YourController');
you also can add exception for those 6 routes. can refer here

Related

What is the difference between those routes?

I define route for update profile logic, when I used first logic It does not work but the use of second logic works fine. So I don't know what is the difference between those.
1. Route::post('/profile', 'ProfileController#update');
2. Route::post('/profile', 'ProfileController#update')->name('profile');
The only difference between them, the name,
so If you put in form action something like {{ route('profile') }} you mean: go to route that has name profile.
Read this for more details.
Routes with name eg Route::post('/profile', 'ProfileController#update')->name('profile');
can be accessed in blade using {{route('profile')}}
whereas the other one can only be accessed using url(). e.g
{{url('/profile')}}
The second one is a 'named route'. It allows you to reference your route by a name.
Laravel 5.7 Docs - Routing - Named Routes
Well the obvious difference is the added "->name('profile')" named route to your second line. You have tagged this post with laravel-5.7 so I have linked the documentation for this version: https://laravel.com/docs/5.7/routing#named-routes
It appears to me that perhaps you have some logic in the update function of your ProfileController like so:
if ($request->route()->named('profile')) {
//
}
Which would change the outcome of the request. Hope this helps, best regards.

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.

Laravel route conflict with parameter

I'm trying to setup routes in Laravel using
Route::get('/post/{id}', 'PostController#index');
Route::get('/post/new', 'PostController#create');
But when I go to mysite.com/post/new its runs the index function thinking its an {id}.
So I'm wondering if I can force /new to go to the create function or if I have to change /post/ to something different.
Thanks in advance for the help!
Also important !! .The order of route declaration matters. Try this
Route::get('/post/new', 'PostController#create');
Route::get('/post/{id}', 'PostController#index');
and you ll notice that your app is able to identify new as a different route from {id}.
That happens because route resolver searches until it finds the first pattern matching the route
Route::get('/post/{id}', 'PostController#index')->where('id', '[0-9]+');
Take a look: Regular Expression Constraints

Design route without Route::controller

Route::controller deprecated in Laravel 5.3,
Now I want to design my routes but I don't know how can I do this without Route::controller because I don't want define Route::get for each route and I want handle the parameters in my controller .
for example, these are my routes :
http://domain.com/images/10/pic.jpg (in this route I need 10 as $file_id and pic.jpg as $file_name)
http://domain.com/images/crop/200x100/10/pic.jpg (in this route I need crop as $action, 200x100 as $size, 10 as $file_id and pic.jpg as $file_name)
How I can do it without Route::controller ?
Currently, the only way to create new routes is to use a resource controller (Route::resource()) or defining your own routes via Route::get,Route::post and so on.
There is no faster and non-deprecated method at the moment. Anyway, the Resource::controller() function might be extracted to another package, as Taylor Otwell stated in his upgrade guide.
I was faced the same dilemma as you, when the Route::controller was dropped with no obvious reason.
This is why I wrote a class called AdvancedRoute, which serves as a drop in replacement.
It can be used by simply replacing Route::controller with AdvancedRoute::controller like this:
AdvancedRoute::controller('customers','CustomerController');
Full information how to install and use find at the GitHub repo at:
https://github.com/lesichkovm/laravel-advanced-route

Laravel 4.x, using both controller & resource routing

I'm trying using this:
Route::resource('users', 'UserController');
Route::controller('users', 'UserController');
When I'm using one of them - WORK,
otherwise - only resource work.
There is an option to using them both?
Which ever is on-top will take priority so if you put Route::controller on-top then that's the one that would work. I would post this as a comment but I don't have the rep to do it. Also why would you wanna use both of them at the same time?
Try moving Route::controller declaration above Route::resource:
Route::controller('users', 'UserController');
Route::resource('users', 'UserController');
The thing is that Laravel tries to match request with your defined routes by going from top to bottom and stops when it finds one.
localhost/users/example in your example actually hits show method in your UserController class as explained in documentation (see Actions Handled By Resource Controller).
Therefore Route::controller('users', 'UserController'); is ignored in this case.
I believe it's only working with one because once you use Route::resource(), all routes starting with users is going to be grabbed, and since Route::resource() does not work by prepending the action with the last segement in the uri (public function getUsers()), it's failing.
With your example provided, all you should need to use is Route::controller(). If there are some cases where that won't do, before it, add whatever routes you need using Route::get(), Route::post() or Route::any()
Route::resource() and Route::controller() were I believe not designed to work together and there shouldn't be much need to actually use them together.

Resources