Laravel Routing - 404 - laravel

I tried different routes but I am getting 404 on show,edit,store.
Auth::routes();
Route::get('/', 'IndexController#index')->name('index')->middleware('user');
Route::get('/user/profile', 'HomeController#index')->name('user')->middleware('user');
Route::get('/{product}/show', 'IndexController#show')->name('product')->middleware('user');
Route::prefix('cart')->group(function () {
Route::get('/', 'IndexController#cart')->name('cart')->middleware('auth');
Route::get('/{product}/add', 'IndexController#cartAdd')->name('cartAdd')->middleware('auth');
});
Route::prefix('dashboard')->group(function () {
Route::get('', 'AdminController#index')->name('admin')->middleware('admin');
Route::get('products', 'ProductController#index')->name('productIndex')->middleware('admin');
Route::get('products/create', 'ProductController#create')->name('productCreate')->middleware('admin');
Route::get('products/{product}', 'ProductController#show')->name('productShow')->middleware('admin');
Route::put('products/{product}', 'ProductController#update')->name('productUpdate')->middleware('admin');
Route::post('products', 'ProductController#store')->name('productStore')->middleware('admin');
Route::get('products/{product}/edit', 'ProductController#edit')->name('productEdit')->middleware('admin');
Route::post('products/{product}', 'ProductController#destroy')->name('productDestroy')->middleware('admin');
});
I'm trying to fix the routes by rearranging but didn't have any luck so far.

Route::get('/{product}/show', 'IndexController#show')->name('product')->middleware('user');
Move this route at the end. Using a dynamic value that soon in a route without any prefix is not the best way to go by the way. Maybe you can prefix it with something static.
Run php artisan optimize before testing it, to reconfig cache and routes.

It seems like it is because of the route key name here
public function getRouteKeyName()
{
return 'prod_name';
}
after deleting it all routes are working again.

Related

Add username as prefix in route URI in Laravel 9

I am building an application where users are registered and want to be redirected to their individual dashboard like this .
http://localhost/project/{username}/dashboard,
Now, its happening like
localhost/project/vendors/dashboard (here all users are accessing same URL)
but I want to make it like :
http://localhost/project/{username1}/dashboard, http://localhost/project/{username2}/dashboard
Googled lot but none of them are explained well and working.
Please assist with complete flow.
I want to declare the value of {username} globally and use it in route as prefix.
I dont want to use it before each name route. will use it as prefix and group with all vendors routes
I have made this, and its working as
localhost/project/vendors/dashboard
Route::prefix('vendors')->group(function () { Route::middleware(['auth:vendor'])->group(function () { Route::get('/dashboard', [VendorController::class, 'dashboard'])->name('vendor.dashboard'); });
});
You can specify route parameters in brackets like so {parameter}, change your code into this.
Route::get('project/{username}/dashboard', [UserDashboardController::class, 'dashboard'])
->name('user.dashboard');
In your controller you could access it like this.
class UserDashboardController
{
public function dashboard(string $username)
{
User::where('username', $username)->firstOrFail();
// something else
}
}
Seems like in your routes your are mixing vendor prefix logic in with something that in your specifications of what your urls should look like does not match. Which i think is making up some of the confusion on this case.
You can use route prefix like this
Route::prefix('{username}')->group(function () {
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [UserController::class, 'dashboard'])->name('user.dashboard');
});
});

Laravel 8 error route | The GET method is not supported for this route

Laravel V 8 Jetstream.
This problem appeared with me after I uploaded and published the project on shared hosting
Error message
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods:
HEAD.
enter image description here
Route::get('/', function () {
return view('welcome');
});
route code
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
Route::get('/dashboard',function () {
return view('dashboard');
})->name('dashboard');;
Route::get('/upload', function () {
return view('uploadpdf');
})->name('upload');
});
If you encounter this problem, always check the settings in your .env and config files
The error was found in config.php - /bootstrap/cache/config.php.
I did not configure the location settings like APP_URL and the paths of some directories were pointing to my computer
Good luck everyone
Greetings from the Dzsecurity company team for helping to find a solution

iam new to laravel 8 Iam creating Routes and they generate an error

am creating route in laravel-8 only one route is working and the others are not why
Routes problem
Route::get('/', function () {
return view('dashboard');
});
only the '/' the first route working and the other 2 which are below are not working. when want to access on teh screen shows that
404 NOT FOUND.
please some one help me.
Route::get('/allProducts', function() {
return "view('products');";
});
Route::get('contact',function(){
return "hello ";
});
First of all, make sure your URL is correct. According to Laravel 8 documents, you have to display it this way. If this problem persists, check your web server configuration.
Route::get('/allProducts', function () {
return view('dashboard');
});
To see which routes are set up run the following form command line.
php artisan route:list
start a development server by running the following in your command line
localhost:8000/allProducts

Laravel router problem when I use Route::any

I would appreciate any help you could give me with the following problem I have.
I am trying to implement a Single Page Application (SPA) with Laravel and Vue, at the moment I am defining the routes, I have something like the following:
routes/app.php
Route::group(['middleware' => 'web'], function () {
Route::any('{path}', function () {
return view('index');
});
});
What I'm trying to do here is the following:
For the middleware web you will only have one view available, in this case in the file index in (resources/views/index.php)
I want that regardless of the method used, Laravel returns the view I want. But I have not succeeded.
Output of php artisan route:list
And when I try to verify with Postman I receive a 404 in response regardless of the method or the URL that I requested .. what could I be doing wrong? Thank you very much in advance.
EDIT 1
I have modified my router and added a conditional as follows:
Route::group(['middleware' => 'web'], function () {
Route::any('{path}', function () {
return view('index');
})->where('path', '.*');
});
Now the GET and HEAD method work properly, however ... when I try the POST method, I get the following error ..
EDIT 2
As mentioned #lagbox in the comments below, error 419 refers to missing CSRF token. I could disable this check in the file Middleware/VerifyCsrfToken.php.
However I wish I could just return the desired view .. without first checking the CSRF token. Another detail that I find is the following:
When in Postman I make a request by a method different from the typical methods ... let's say UNLINK method, I get the following result:
Which leaves me a bit confused, when I define in my routes file web.php Route::any, does it mean any route or not?

is it possible to define 2 separate route pointing to one resource controller?

i am having a challenge with my route. is it possible to define 2 separate route pointing to one resource controller?
Route::prefix('artist')->middleware('role:artist')->group(function () {
Route::get('/', 'ArtistController#index');
Route::get('/dashboard', 'ArtistController#dashboard')->name('artist.dashboard');
Route::resource('/mp3', 'Mp3Controller');
});
Route::resource('/mp3', 'Mp3Controller');
Yes, you can do this. In this case these URIs will be pointed to the same method:
artist/mp3
mp3
Run this command to see all the routes generated by your code:
php artisan route:list
Route::get('/abc', 'LandingController#index');
Route::get('/xyz', 'LandingController#index');
index(){
return view('landing')
}
both /abc and /xyz will load the view called "landing"

Resources