Product And Category Separation In Route (Laravel) - 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.

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') }}

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.

Redirects to same action from different urls in laravel 5.2

I am using Laravel 5.2.
I am passing parameters via two urls. But it always goes to the first route only.
code in my routes.php is as follows:
Route::get('{department}', 'settings#load_department_settings')->middleware('auth');
Route::get('{id}', 'settings#load_staff')->middleware('auth');
Anchor tags in view is as follows:
<td>{{$staff->name}}</td>
<td>{{$staff->deptname}}</td>
The issue is it always uses 'settings#load_department_settings'. How could i use 'settings#load_staff' when clicking on staff name?
It is always going to use the {department} route because that is matching anything on the root / slash. You need tto dive at least one of them unique prefixes. For example:
Route::get('department/{department}', 'settings#load_department_settings')->middleware('auth');
Route::get('staff/{id}', 'settings#load_staff')->middleware('auth');
Then
<td>{{$staff->name}}</td>
<td>{{$staff->deptname}}</td>

Laravel 4 routing: Cannot use variable name more than once

I'm having an issue with Laravel 4 routing. I am trying to create two sets of routes:
domain.com/meetings/aa, al-anon, etc.
domain.com/meetings/day/sun, mon, tue, etc.
Here is what I am specifying in my routes file:
Route::resource('meetings/day/{dayName}', 'Meetings_DayController');
Route::resource('meetings/{fellowshipName}', 'Meetings_MeetingController');
I need to be able to pass variables to my resourceful controllers. But I am getting this kind of an error, no matter what order I put the routes in:
Route pattern "/meetings/fellowship/{fellowshipName}/{{fellowshipName}}" cannot
reference variable name "fellowshipName" more than once.
Not only that, but those two routes cause other, undeclared routes to give the same error. My 404 route doesn't kick in for some reason. Ideas, anyone? I've tried everything I can think of. If I could match a three-letter string with the day controller route, that would work, but I can't figure out the regex for a three-letter string in Laravel. \w{3} doesn't work.
You are using Route::resource incorrectly. You can't (and should not) pass a variable to a resource controller. Instead You will need to declare a new route with the verb you need and the parameter. Route::resource only creates several pre-handled routes for you for quick CRUD RESTful access
See the answer to similar situation here: https://stackoverflow.com/a/19608572/385402

Resources