What is the difference between those routes? - laravel

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.

Related

how to use two functions from same controller in single page route using get in laravel

Am trying to use two different functions from one controller in a single page route
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
But the problem is the function alldata works where the function index doesn't
You can't have 2 GET routes with the same path.
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart/all','App\Http\Controllers\Frontend\CartController#alldata');
The /cart route is overwritten by the alldata(). So the alldata() is calling instead of index().
kindly remove the alldata()'s route and pass the data from index().
Route::get('/cart','App\Http\Controllers\Frontend\CartController#index');
Route::get('/cart','App\Http\Controllers\Frontend\CartController#alldata');
Try to manipulate your logic in controller rather than in route file.
Use conditional in controller function.

What is reverse routing conceppt in laravel

Please, anyone, explain what is reverse routing with example.
I'm searching this question but still confused about this reverse routing concept.
For example the following route declaration tells Laravel to execute the action “signUp” in the controller “UsersController” when the request’s URI is ‘signUp’.
http://mycoolsite.com/signUp
Route::any('signUp’, 'UsersController#register’);
Traditionally, we may link to the registration page like this:
{{ HTML::link('signUp’, 'Register Now!’) }}
However, this has the unfortunate disadvantage of being dependent on our route declaration. If we change the route declaration to:
http://mycoolsite.com/signup
Route::any('register’, 'UsersController#signUp’);
Then our link will be wrong. We’ll have to go throughout our entire site and fix our links. Hope we don’t miss one!
Instead, let’s use reverse-routing.
{{ HTML::link_to_action('UsersController#signUp’, 'Register Now!’) }}
Now, the link that we generate will automatically change when we change our routing table. In our first example it’d generate http://mycoolsite.com/register. Then, when we change the routes call to match our second example it’ll generate http://mycoolsite.com/signup.
In traditional routing you depend on route declaration. In reverse routing on the some action(method, function)
In the route file, we define every route's name and use a complete website that routes names now in the future if we want to change a route URL then easily change it from the routes file. We change only one place and it applies the whole website by route names thus reverse routing makes development fast and flexible.
in Laravel 8, 9
Route::get('users', [UserController::class, 'index'])->name('user.index');
now link generate by route name
{{ route('user.index') }}

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

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