Why are new routes in Laravel not working? - php

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

Related

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

Laravel throwing 404 for an existing route

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

Post method not working when I use postman for run API In laravel 5.1 API

Controller code: public function xyz(){echo 'hello';}
Route::group(['prefix' => 'api'], function(){Route::post('apiregstration','APIcontroller#xyz');});
I use laravel 5.1 and want to create API with post method but it not work ,
GET method work fine
If get is work but post not working, maybe you should try to run php artisan route:clearrun to clear route caches.
Looks like there is a typo in your snippet (which I'm assuming is coming directly from your source).
Try changing
Route::group(['prefix' => 'api'], function() {
Route::post('apiregstration','APIcontroller#xyz');
});
To
Route::group(['prefix' => 'api'], function() {
Route::post('apiregistration','APIcontroller#xyz');
});
I'm assuming you meant 'apiregistration' and not 'apiregstration'.
There is not much we can assume or test or check with the info you provided.
If the error doesn't go away, try adding a bit more of your code in the question so we can help.

Form validation is not displaying results on laravel

My form validation was not displaying errors on my posts/create route. I Googled the solution and found that
Route::group(['middleware' => 'web'], function() {
Route::resource('/posts','PostsController');
});
can be changed to
Route::group(['middlewareGroups' => 'web'], function() {
Route::resource('/posts','PostsController');
});
I did that and the problem is solved now. I want to ask if this is considered a good practice to change it like this?
Also if I am removing this line from my routes it is working:
Route::group(['middleware' => 'web'], function() {
Can anyone tell me what is actually happening ?
One reason can be that web middleware is applying two times in your case. Laravel by default adds web Middleware in app/Providers/RoutesServiceProvider.php and again you are adding it in your routes.php
Try it by removing web middleware from your routes.php.
Well, Laravel 5.3 documentation explains it in detail.
Here is the link.
https://laravel.com/docs/5.3/middleware#middleware-groups
But to answer your question, this line:
Route::group(['middleware' => 'web'], function() {
doesn't allow any request to access the routes inside only if they validate a condition which is inside the "web" middleware.
This is how laravel works. The doc gives more detail about each component in the framework.

Laravel NotFoundHttpException (RouteCollection->match)

I am new to Laravel and was following a tutorial (https://www.youtube.com/watch?v=Zz_R73eW3OU&list=PL09BB956FCFB5C5FD). I have just installed the latest version of Laravel and am going through the beginning stages of the tutorial. So far (installation, creating a migration) I was doing ok, but now I am stuck at the point of creating new routes.
I have created a controller in app/controllers:
<?php
class Slots_Controller extends Base_Controller{
public $restful = true;
public function get_index(){
return View::make( 'slots.index' );
}
}
As well as a very minimalist view in the file app/views/slots/index.php
<h1>Slots</h1>
My app/routes file looks like this :
Route::get('/', function()
{
return View::make('hello');
});
Route::get( '/public/slots', array(
'uses' => 'slots#index'
) );
I've searched a number of things on the web and here is how far I have come :
- i know ideally only the public dir should be in the htdocs, i will be doing that presumably at a further stage of the tutorial. so as of now, my laravel home page is at http://localhost:8888/my_directory.laravel/public/
- i have checked the apache mod-rewrite : it is on and it works well for codeIgniter
- i have checked that the .htaccess is as provided by laravel (and as suggested by different posts)
The weird thing is, if i change the routes for the home page like this :
Route::get('/', function()
{
return 'hello world';
// return View::make('hello');
});
Nothing changes, i.e. i still get the original welcome page. My understanding is this modification should have simply output "hello world" as the html for the home page.
So it seems that whatever I'm doing in routes.php has no effect whatsoever, as if I was modifying the wrong file or something.
I've spent the most part of an otherwise sunny afternoon on this, any help is much apreciated :)
Thanks,
Tepp
There are many things you are doing wrong here (The best way is to follow the tutorial on Laravel):
In your controller file you are mentioning "Base_Controller", are you sure you have such a file in your controllers directory? (According to my knowledge it should be BaseController)
The Route::get is not defined properly. There are many ways to define a route; however for your case it should be something like this:
Route::get('slots', array(
'uses' => 'Slots_Controller#get_index'
));
Hope this helps.

Categories