Laravel throwing 404 for an existing route - php

Can someone help me understand what is wrong with these routes:-
From the list of these routes, the third and the last one returns 404. There is no issue with the controllers. They show up as expected when I run php artisan route:list.
Route::get('/uploads', 'ImageController#adminIndex')->name('admin.images.index');
Route::get('/uploads/{image}', 'ImageController#adminShow')->name('admin.image.indivisual');
Route::get('/uploads/request', 'ImageController#imageRequests')->name('admin.images.request');
Route::get('/uploads/request/{image}', 'ImageController#individualRequest')->name('admin.images.request.individual');
Route::post('/uploads/accept', 'ImageController#acceptImage')->name('admin.accept.image');
Route::post('/uploads/decline/', 'ImageController#declineImage')->name('admin.decline.image');
Route::get('/uploads/all', 'ImageController#index')->name('admin.images.list');
The thing that confuses me is that changing uploads to images for these two routes solved the problem and they work just fine.
Route::get('/uploads', 'ImageController#adminIndex')->name('admin.images.index');
Route::get('/uploads/{image}', 'ImageController#adminShow')->name('admin.image.indivisual');
Route::get('/images/request', 'ImageController#imageRequests')->name('admin.images.request');
Route::get('/uploads/request/{image}', 'ImageController#individualRequest')->name('admin.images.request.individual');
Route::post('/uploads/accept', 'ImageController#acceptImage')->name('admin.accept.image');
Route::post('/uploads/decline/', 'ImageController#declineImage')->name('admin.decline.image');
Route::get('/images/all', 'ImageController#index')->name('admin.images.list');
I have tried php artisan route:clear.
Also, there are no folders in the public directory to create any conflicts.
Note: All the routes are grouped in
Route::group(['prefix' => 'admin', 'middleware' => 'role:administrator|auth'], function () {
// Other routes in this group are working just fine. No issues.
});
Appreciate the help.

Please move the router into last of list:
Route::get('/uploads/{image}', 'ImageController#adminShow')->name('admin.image.indivisual');
Because It includes Route::get('/uploads/request' and Route::get('/uploads/all' then It override this two routers
So code of routers list:
Route::get('/uploads', 'ImageController#adminIndex')->name('admin.images.index');
Route::get('/uploads/request', 'ImageController#imageRequests')->name('admin.images.request');
Route::get('/uploads/request/{image}', 'ImageController#individualRequest')->name('admin.images.request.individual');
Route::post('/uploads/accept', 'ImageController#acceptImage')->name('admin.accept.image');
Route::post('/uploads/decline/', 'ImageController#declineImage')->name('admin.decline.image');
Route::get('/uploads/all', 'ImageController#index')->name('admin.images.list');
// move to last
Route::get('/uploads/{image}', 'ImageController#adminShow')->name('admin.image.indivisual');

Related

How to call route which is not defined in laravel

I am newbie to laravel and i am working on a project and i have a following situation
lets assume my base url is https://example.com
Now i want to pass a slug(argument) after a base url which means https://example.com/xyz something like that, and i need to do this on multiple times in my project
This is what i'd tried but it is not working it says that route is not defined.
Route::get('{slug?}', [App\Http\Controllers\UiviewsController::class, 'method1'])->name('method1');
Route::get('/method2/{slug?}', function($slug){
return redirect()->route('method1', ['slug'=>$slug]);
});
And also how can i achieve that on which argument which particular method should be called? for example if i have several other routes similar to above one.
how can i achieve this?
Thank you in advance for your help. :)
You should use the fallback system.
Route::fallback(function () {
//
});
Laravel Route fallback official docs
also, beware:
The fallback route should always be the last route registered by your
application.
Other Option:
Also, you can define a parameter as below example
Route::any('{any}', function(){
//...
})->where('any', '.*');
Please Try php artisan route:cache in your terminal then check it again.

Out of a sudden my /admin route inside the Auth Middleware in Laravel 8 returns a 404 not found

I recently started to code my own Admin panel in Laravel.
Every route was working fine, but all of a sudden the /admin route inside the Auth middleware group stopped working properly.
This are my routes inside web.php
My php artisan route:list
And the EntryController#index looks like this:
public function index()
{
//
$entries = Entry::all();
return view('admin.index', ['entries' => $entries]);
}
I'm having this problem for about 2 now, so maybe one of you know the solution.
I think you're having this issue because of how Laravel prioritises its routes.
And I think the culprit might be this route:
Route::get('/{link}', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
When you use {link} you are basically saying: "expect anything in this segment of the URI". Since the /{link} route is placed before the /admin route, and their URIs both contain only one segment, Laravel will try to resolve /{link} first.
Solution: just move the /{link} route below the /admin route. Might be best to just place it at the bottom of the list :D

Unable to find route name if added to prefix in Laravel

The below route works fine.
Route::get('/create','HolidaysController#create')->name('createHoliday');
However, it I get the
Route [createHoliday] not defined
error when I place it in a route prefix.
Route::prefix('holidays')->group(function () {
Route::get('/create','HolidaysController#create')->name('createHoliday');
});
I have cleared all config, cache, route and views through the artisan command without any luck.
Cant figure out why it's not working.
using Laravel 7.24
When we use prefix in routes , the prefix also added in the route name
Please try
holidays.createHoliday

Why are new routes in Laravel not working?

I have been working on this project of mine now for sometime and has no problem with routes until today.
I have even tried clearing cache and dump autoloading. Nothing seems to be working.
I tried to add a new route today and i got a 404 error. I have also use "get" and "any", all to no avail.
At first, I have tried creating several new routes and I'm still getting the same 404 error. Below is how a part of my web.php looks like.
Route::group(['middleware' => ['auth', 'role:teacher']], function () {
Route::any('/testing', 'PagesController#testing');
Route::resource('/attendance', 'AttendanceController');
Route::get('/teacher/dashboard', 'TeachersController#dashboard')->name('teacher.dashboard');
Route::resource('homework', 'HomeworkController');
Route::resource('/teacher/events', 'EventsController',['names' =>
'teacher.events']);
Route::any('/view_students', 'StudentsController#myStudents')->name('view.students');
Route::resource('results', 'CoursesResultController');
Route::get('/results/class_course/{id}', 'CoursesResultController#showCourseResult');
Route::post('/results/class_course/{id}', 'CoursesResultController#saveCourseResult');
});
EDIT: I have solved the issue. I had to delete the cache files in the bootstrap folder manually. Thanks, guys.
Replace your code with the following:
Route::resource() generates all the possible routes for you, hence should be kept as the last possible point.
Route::group(['middleware' => ['auth', 'role:teacher']], function () {
Route::any('/testing', 'PagesController#testing');
Route::resource('/attendance', 'AttendanceController');
Route::get('/teacher/dashboard', 'TeachersController#dashboard')->name('teacher.dashboard');
Route::resource('homework', 'HomeworkController');
Route::resource('/teacher/events', 'EventsController',['names' =>
'teacher.events']);
Route::any('/view_students', 'StudentsController#myStudents')->name('view.students');
/* changes over here
`Route::resource()` generates all the possible routes for you, hence should be kept as the last possible point.
*/
Route::get('/results/class_course/{id}', 'CoursesResultController#showCourseResult');
Route::post('/results/class_course/{id}', 'CoursesResultController#saveCourseResult');
Route::resource('results', 'CoursesResultController');
});

Laravel 5.1 - Route doesnt work

i have a problem with routes, i have my route:
Route::get('dashboard/password', 'UserController#password');
Route::post('dashboard/updatepassword', 'UserController#updatePassword');
// PAGINA UTENTE PUBBLICA
Route::get('/{username}', 'FrontController#user');
// blog routes
Route::get('blog', 'FrontController#blog');
Route::get('blog/{slug}', 'FrontController#article');
Route::get('blog/category/{name}', 'FrontController#BlogCategory');
Route::get('blog/tag/{name}', 'FrontController#tags');
Route::resource('comment', 'CommentController');
and my FrontController:
public function blog()
{
$articles = Article::OrderBy('id','DESC')->paginate(3);
$Allarticles = Article::OrderBy('id','DESC')->get();
$Allcategories = BlogCategory::OrderBy('id','DESC')->get();
$Alltags = Tag::OrderBy('id','DESC')->get();
$Allcomments = Comment::OrderBy('id','DESC')->take(3)->get();
return view('blog', compact('articles','Alltags','Allarticles','Allcategories','Allcomments'));
}
if i go to "http://localhost:8000/blog" it return to page where i was before. similar to route->back().
I dont know why i have this problem, other blog routes work well.
I done some test like this:
public function blog()
{
return "Hi";
}
it doesnt return "Hi", so i think is a problem with the route. i have not anable middleware here, my other routes like blog/article work well.
Could you post the contents of your routes file?
If there's any routes for 'blog' above the one you've posted which contain a parameter (eg. Route::get('blog/{blog_post_id}, ...), try moving them below 'blog' in the file.
If it isn't the above then it sounds like there might be some caching at play that's screwing around with stuff, it routinely catches me out when i'm running my optimisations to see how the production environment will perform and i forget to clear all the caches, Here's my usual fixes (that i've got aliased because i mess this up so often);
php artisan route:clear
php artisan view:clear
php artisan cache:clear (Side note, clears all auth sessions, will require a re-log)
composer dump-autoload
php artisan optimize --force
This will completely clear any caches that have been created for routes, views and authorisation.
Also check your Laravel logs and your Apache/NginX logs as well, always worth having a look in those
Your issue is pattern matching in the routes file. It seems that Routes are assigned to the first route that matches the URI.
Route::get('/{username}', 'FrontController#user');
Route::get('blog', 'FrontController#blog');
http://localhost:8000/blog matches both these routes because the {username} could be blog and thus Route::get('/{username}', 'FrontController#user'); will always be used.
You must either be more specific in the route name (e.g. add more text) or more specific in the order of the routes. Here is an example with your current routes ordered in the way you would want.
Route::get('dashboard/password', 'UserController#password');
Route::post('dashboard/updatepassword', 'UserController#updatePassword');
// blog routes
Route::get('blog', 'FrontController#blog');
Route::get('blog/{slug}', 'FrontController#article');
Route::get('blog/category/{name}', 'FrontController#BlogCategory');
Route::get('blog/tag/{name}', 'FrontController#tags');
Route::resource('comment', 'CommentController');
// PAGINA UTENTE PUBBLICA
Route::get('{username}', 'FrontController#user');

Categories