how to use two functions from same controller in single page route using get in laravel - 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.

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

codeigniter with same route name with different controller functions

I want to keep routes like this
$route["signup"] = "Controller/signup";
$route["signup"] ="Controller2/fbsignup";
Is it possible to to give same route names with different controller functions.
Then how ?
No, you can't.
If you write same route again, it will overwrite first one.
It is just like an array variable.
If you assign any other value to variable, first one will overwrite.
But you can specify HTTP method with route.
No you can't do like this, you are going to make api then define method that will help
$route["signup"]["post"] = "Controller/signup";
$route["signup"]["GET"] ="Controller2/fbsignup";
I want to keep routes like this
$route["signup"] = "Controller/signup";
$route["signup"] ="Controller2/fbsignup";
then try this in routes and url should be like signup/signup and signup/fbsignup
$route["signup/signup"] = "Controller/signup";
$route["signup/fbsignup"] ="Controller2/fbsignup";
call the url like
signup/signup
signup/fbsignup

MVC subController

My application has the following module:
/groups/{id_group} -- > GroupController.php
My question is this,
/groups/{id_group}/events/{id_event}
Which controller should handle this?
If you are using groups/n/ as routing, you would want to use your framework's routing system to route to the event controller. Otherwise, it is the groups controller.
If you want to stick to just using the first parameter as the Controller, then consider using the further parameters as purely parts of the query string e.g
?controller=groups&itemId={id_group}&action=events&actionId={id_event)
and use the controller to route it to the correct place e.g.
$model->getGroup($id_group, 'events', $id_event);

Resources