Getting same page on every URL/Route in Laravel8 - laravel

I'm using default laravel auth , I have multiple routes with prefix group but when I use other routes/urls but giving the same page on every route which is View page the product
you can see my route
Auth::routes();
Route::prefix('products')->group(function () {
Route::get('/', [ProductsController::class, 'index']);
Route::get('/{code}', [ProductsController::class,'view_product'])->name('products.view_product');
});
On this below route giving the product_view page which I can't understand why is giving
Route::get('/test_route', , function () {
return 'Hello World';
});
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
| Domain | Method | URI | Name | Action
| Middleware |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+
| | GET|HEAD | api/user | generated::suiIeX6oHZWSnSP1 | Closure
| api |
| | | | |
| App\Http\Middleware\Authenticate:sanctum |
| | GET|HEAD | sanctum/csrf-cookie | generated::WKOquUKjM8nfHvKl | Laravel\Sanctum\Http\Controllers\CsrfCookieController#s
how | web |
| | GET|HEAD | products | generated::IucNuoXqqYtZPi70 | App\Http\Controllers\ProductsController#index
| web | |
| | GET|HEAD | {code} | products.view_product | App\Http\Controllers\ProductsController#view_product
| web |
+--------+----------+--------------------------+-----------------------------+--------------------------------------------------------
----+------------------------------------------+

Related

laravel named resources not working properly and removes the dots from the beginning

problem:
I'm trying to add name prefixes to my route groups:
Route::middleware(['auth:sanctum', 'verified'])->prefix('dashboard')->name('dashboard')->group(function () {
Route::get('', [DashboardController::class, 'dashboard']);
// other routes
Route::resource('estates', EstateController::class, ['names' => '.estates' ])->except('show');
// other routes
});
it removes the .. (it should be dashboard.estates.index , etc ...)
| POST | dashboard/estates | dashboardestates.store |
| GET|HEAD | dashboard/estates | dashboardestates.index |
| GET|HEAD | dashboard/estates/create | dashboardestates.create |
| DELETE | dashboard/estates/{estate} | dashboardestates.destroy |
| PUT|PATCH | dashboard/estates/{estate} | dashboardestates.update |
| GET|HEAD | dashboard/estates/{estate}/edit | dashboardestates.edit |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test 1:
how ever if I pass an array option to names it works fine with dots ..
Route::resource('estates', EstateController::class, ['names' => ['index' => '.estates.index'] ])->except('show');
| POST | dashboard/estates | dashboardestates.store |
| GET|HEAD | dashboard/estates | dashboard.estates.index |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test 2:
// web.php
Route::resource('estates', EstateController::class, ['names' => '.estates.' ])->except('show');
routes list:
| POST | dashboard/estates | dashboardestates..store |
| GET|HEAD | dashboard/estates | dashboardestates..index |
| GET|HEAD | dashboard/estates/create | dashboardestates..create |
| DELETE | dashboard/estates/{estate} | dashboardestates..destroy |
| PUT|PATCH | dashboard/estates/{estate} | dashboardestates..update |
| GET|HEAD | dashboard/estates/{estate}/edit | dashboardestates..edit |
expected: dashboard.estates..index, etc
got: dashboardestates.index, etc
You can simplify it all to as follows:
Route::group(['middleware' => ['auth:sanctum', 'verified']], function () {
Route::get('/dashboard', [DashboardController::class, 'dashboard'])
->name('dashboard');
Route::group(['prefix' => '/dashboard', 'as' => 'dashboard.'], function () {
Route::resource('estates', EstateController::class)->except('show');
});
});
The output of the above for named routes will be dashboard.estates.{method}.
This could be simplified (i.e. not repeating or nesting certain things) the named route for the dashboard were to be dashboard.index rather than dashboard.

Laravel Route not defined error on FP_route

I'm having a problem with a POST, when I try to run the page and data is loaded in the database table I'm trying to read I always get a Route [MAILFP_Tree] not defined error.
The code is triggered by a call for the route in the blade file that will visualize the page
<form action="{{route('MAILFP_Tree')}}" method="post">
the routes are the following
Route::get('FP_Tree', 'HomeController#toFP_Tree')->middleware('auth');
Route::post('FP_Tree', 'MailController#toFP_Tree')->middleware('auth')->name('MAILFP_Tree');
This is the controller created for the post method (it's the one called mailController)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \Illuminate\Support\Facades\Mail;
use App\Mail\emailFP_TreeRow;
use DB;
use Auth;
use App\AssociationHistorical;
class MailController extends Controller
{
public function toFP_Tree(Request $request)
{
$order_number = $request->input('order_number');
$customer_name = $request->input('customer_name');
$date_order_placed = $request->input('date_order_placed');
$item_number = $request->input('item_number');
$quantity = $request->input('quantity');
$item_description = $request->input('item_description');
$passed = array($order_number, $customer_name, $date_order_placed, $item_number, $quantity, $item_description);
Mail::to(Auth::user()->email)->send(new emailFP_TreeRow($passed));
//passes data to blade
$rows = AssociationHistorical::all();
return view('FP_Tree',[
'rows'=>$rows
]);
}
}
this is the content of the homecontroller
public function toFP_Tree()
{
//passes data to blade
$rows = AssociationHistorical::all();
return view('FP_Tree',[
'rows'=>$rows
]);
}
This is my migration table
Schema::create('association_historical', function (Blueprint $table) {
$table->id();
$table->string('order_number');
$table->string('item_number');
$table->string('item_description');
$table->date('date_order_placed');
$table->string('customer_name')->nullable();
$table->string('sales_rep_email');
});
The thing is that the problem happens when there is data in the association_historical table, most likely It is related to the fact that if there is no data in the database it won't run the post method, but i thought that it would make sense to show the database table...
the strange thing is that on someone else computer this works without any problem.
This is the result from PHP artisan route:list
+--------+----------+--------------------+-----------------------------+----------------------------------------------------------+------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+--------------------+-----------------------------+----------------------------------------------------------+------------+
| | GET|HEAD | / | generated::UYk0LlF0PlICNJsK | App\Http\Controllers\LoginPageController#showLogin | web |
| | POST | / | login | App\Http\Controllers\LoginPageController#doLogin | web |
| | GET|HEAD | FP_Tree | generated::98bieUMU1hfvx1TH | App\Http\Controllers\HomeController#toFP_Tree | web |
| | | | | | auth |
| | GET|HEAD | Prediction | generated::4HdessOBvQL3KmpT | App\Http\Controllers\HomeController#toPrediction | web |
| | | | | | auth |
| | GET|HEAD | PredictionsLanding | generated::Xtf7I1MCKtlOSX3Y | App\Http\Controllers\HomeController#toPredictionsLanding | web |
| | | | | | auth |
| | GET|HEAD | UserManagement | generated::aOYFVG9JOLTfGjB5 | App\Http\Controllers\HomeController#toUserManagement | web |
| | | | | | auth |
| | GET|HEAD | api/user | generated::3SA9mFdg4RMncH1a | Closure | api |
| | | | | | auth:api |
| | GET|HEAD | logout | generated::YeCPB1fIxlH8JqDe | App\Http\Controllers\HomeController#doLogout | web |
| | | | | | auth |
+--------+----------+--------------------+-----------------------------+----------------------------------------------------------+------------+
it was so simple, thanks DigitalDrifter, just had to do a php artisan route:clear

Route returning Error 404 but the route exists

I have a blade template where I call a route to the show method:
<div class="user"><div><img src="{{asset('images/user.svg')}}" alt=""></div></div>
That route is controlled by a resource controller and the routes are set like this:
Route::resource('profile', 'App\Http\Controllers\ProfileController')->middleware('auth');
All the routes of that controller are defined as you can see from the output of my php artisan route:list:
| | GET|HEAD | profile | profile.index | App\Http\Controllers\ProfileController#index | web |
| | | | | | auth |
| | POST | profile | profile.store | App\Http\Controllers\ProfileController#store | web |
| | | | | | auth |
| | GET|HEAD | profile/create | profile.create | App\Http\Controllers\ProfileController#create | web |
| | | | | | auth |
| | GET|HEAD | profile/{profile} | profile.show | App\Http\Controllers\ProfileController#show | web |
| | | | | | auth |
| | DELETE | profile/{profile} | profile.destroy | App\Http\Controllers\ProfileController#destroy | web |
| | | | | | auth |
| | PUT|PATCH | profile/{profile} | profile.update | App\Http\Controllers\ProfileController#update | web |
| | | | | | auth |
| | GET|HEAD | profile/{profile}/edit | profile.edit | App\Http\Controllers\ProfileController#edit | web |
| | | | | | auth |
So far the controller only has a dd function since I haven't started writing it yet and the show method is the only one with anything written yet:
public function show($id)
{ dd($id);}
But for some reason, whenever I try acessing that route, it returns a 404 Not Found error. I tried accessing the index method and it works, but the show method always returns the 404 Not Found error. I also tried php artisan route:clear but it didn't solve the problem. What could I be doing wrong here?
Try out it , list all of router
php artisan route:list
Or you can as such that
Route::resource('profile', 'ProfileController', [
'names' => [
'index' => 'show.show'
'store' => 'profile.new',
]
]);
And if you try this ?
<div class="user"><div><img src="{{asset('images/user.svg')}}" alt=""></div></div>

Laravel 7 Route Group return 404 on some related routes

Route::group(['prefix'=>'cart'], function (){
Route::get('', 'CartController#index')->name('cart.index');
Route::get('{id}', 'CartController#create')->name('cart.create')->middleware('auth');
Route::any('update/{id}/{qty}', 'CartController#update')->name('cart.update')->middleware('auth');
Route::any('saveorder', 'CartController#store')->name('cart.store')->middleware('auth');
Route::any('delete/{rowId}', 'CartController#destroy')->name('item.delete')->middleware('auth');
Route::any('empty', 'CartController#empty')->name('cart.empty')->middleware('auth');
});
Some of the routes still works but some of them are broken and returning 404 even that the routes exist
the broken routes are : item.delete , cart.empty , cart.store
| auth:api |
| | GET|HEAD | cart | cart.index | App\Http\Controllers\CartController#index | web |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | cart/delete/{rowId} | item.delete | App\Http\Controllers\CartController#destroy | web |
| | | | | | auth |
| | GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS | cart/empty | cart.empty | App\Http\Controllers\CartController#empty | web |
| | | | | | auth |
| | POST | cart/saveorder | cart.store | App\Http\Controllers\CartController#store | web |
| | | | | | auth |
| | PUT | cart/update/{id}/{qty} | cart.update | App\Http\Controllers\CartController#update | web |
| | | | | | auth |
| | GET|HEAD | cart/{id} | cart.create | App\Http\Controllers\CartController#create | web |
| | | | |
if you're using a GET method with cart.empty and cart.store then they will be handled by cart.create, you should put those routes first (order matters), also its better to be explicit with the route methods instead of using Route::any
Route::prefix('cart')->group(function () {
Route::get('', 'CartController#index')->name('cart.index');
Route::any('saveorder', 'CartController#store')->name('cart.store');
Route::any('empty', 'CartController#empty')->name('cart.empty');
Route::any('delete/{rowId}', 'CartController#destroy')->name('item.delete');
Route::any('update/{id}/{qty}', 'CartController#update')->name('cart.update');
Route::get('{id}', 'CartController#create')->name('cart.create');
});
Route::group(['prefix' => 'cart', 'middleware' => ['auth']], function(){
Route::get('/', 'CartController#index')->name('cart.index');
Route::get('/{id}', 'CartController#create')->name('cart.create');
Route::any('/update/{id}/{qty}', 'CartController#update')->name('cart.update');
Route::any('/saveorder', 'CartController#store')->name('cart.store');
Route::any('/delete/{rowId}', 'CartController#destroy')->name('item.delete');
Route::any('/empty', 'CartController#empty')->name('cart.empty');
});
Route::prefix('cart')->group(function (){
Route::middleware(['auth'])->group(function(){
Route::get('{id}', 'CartController#create')->name('cart.create');
Route::put('update/{id}/{qty}', 'CartController#update')->name('cart.update');
Route::post('saveorder', 'CartController#store')->name('cart.store');
Route::delete('delete/{rowId}', 'CartController#destroy')->name('item.delete');
Route::post('empty', 'CartController#empty')->name('cart.empty');
});
Route::get('/', 'CartController#index')->name('cart.index');
});
Two things to consider. Your cart.index could potentially match URI's underneath the route. For security, please try not to use Route::any if possible. As explained in https://laravel.com/docs/7.x/routing. As #Sobir suggests, run php artisan route:list and see your actual list of routes as Laravel sees it.
If something is missing or ambiguous, you may have to reconsider some route parameters to avoid confusion. Or regroup them in different ways. Your route list will certainly grow. Anticipate on what you're planning in the future of what you are building.

Insert variable into named route using Redirect::route()

I am trying to redirect a route to a named router that contains a variable. Somehow the URL gets resolved into http://localhost:8000/users/reset_password?token=f3c6e64d3d5147fde8843af831ca4998 instead of http://localhost:8000/users/reset_password/f3c6e64d3d5147fde8843af831ca4998 (mind the ?token=)
Because of this mismatch in the URL that gets created, the incorrect route is used.
The Redirect::route() code looks like this:
return Redirect::route('users.reset', array('token'=>$input['token']))
->withInput()
->with('error', $error_msg);
My routes are defined as follows:
Route::get('users/confirm/{code}', array('as' => 'users.confirm', 'uses' => 'UsersController#getConfirm'));
Route::get('users/forgot_password', array('as' => 'users.forgot', 'uses' => 'UsersController#getForgot'));
Route::post('users/forgot_password', array('as' => 'users.forgot', 'uses' => 'UsersController#postForgot'));
Route::get('users/reset_password/{token}', array('as' => 'users.reset', 'uses' => 'UsersController#getReset'));
Route::post('users/reset_password', array('as' => 'users.reset', 'uses' => 'UsersController#postReset'));
Route::resource('users', 'UsersController');
The command php artisan routes gives the next output:
+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+
| Domain | URI | Name | Action | Before Filters | After Filters |
+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+
| | GET|HEAD users/confirm/{code} | users.confirm | UsersController#getConfirm | | |
| | GET|HEAD users/forgot_password | users.forgot | UsersController#getForgot | | |
| | POST users/forgot_password | users.forgot | UsersController#postForgot | | |
| | GET|HEAD users/reset_password/{token} | users.reset | UsersController#getReset | | |
| | POST users/reset_password | users.reset | UsersController#postReset | | |
| | GET|HEAD users | users.index | UsersController#index | | |
| | GET|HEAD users/create | users.create | UsersController#create | | |
| | POST users | users.store | UsersController#store | | |
| | GET|HEAD users/{users} | users.show | UsersController#show | | |
| | GET|HEAD users/{users}/edit | users.edit | UsersController#edit | | |
| | PUT users/{users} | users.update | UsersController#update | | |
| | PATCH users/{users} | | UsersController#update | | |
| | DELETE users/{users} | users.destroy | UsersController#destroy | | |
+--------+---------------------------------------+------------------+----------------------------------+----------------+---------------+
The problem is you have two routes defined with the same name. Either your POST and GET routes have the same users.reset name. And the second one is overwriting the first. Change your name for POST route and you will be fine.
I think the problem is that you also define the name of the variable, you only need to define the variables itself in the correct order:
Redirect::route('users.reset', array($input['token'])
->withInput()
->with('error', $error_msg);

Resources