New Laravel Routes not working
Asked Answered
S

5

14

I have a problem, new routes in laravel are not working, url shows the correct route but almost as if it does not get to my routes web file just returns page not found every time.

I have tried: using named route, moving function to different controller, clearing route cache, clearing app cache, dump-auto load, made sure that AllowOverride is set to All,

Web.php:

    <?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

/*
|--------------------------------------------------------------------------
| Courses
|--------------------------------------------------------------------------
*/
Route::get('/courses', 'CourseController@index');
Route::get('/courses/create', 'CourseController@create');
Route::get('/courses/{course}', 'CourseController@show');
Route::get('/courses/{course}/edit', 'CourseController@edit');
Route::post('/courses', 'CourseController@store');
Route::patch('/courses/{course}', 'CourseController@update');
Route::delete('/courses/{course}', 'CourseController@destroy')->name('course-delete');

Route::get('/courses/statistics', 'CourseController@statistics');

/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/section/{section}', 'SectionController@show');


/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/progress', 'UserProgressController@index');
Route::get('/progress/create', 'UserProgressController@create');
Route::get('/progress/{section}', 'UserProgressController@show');
Route::get('/progress/formativeresults', 'UserProgressController@formativeresults');
//Route::get('/progress/coursestatistics', 'UserProgressController@coursestatistics');
//Route::get('/progress/{progress}/edit', 'UserProgressController@edit');
Route::post('/progress', 'UserProgressController@store');
//Route::patch('/progress/{progress}', 'UserProgressController@update');
//Route::delete('/progress/{progress}', 'UserProgressController@destroy')->name('progress-delete');

Controller:

public function statistics()
    {
        dd('Test');
       return view('coursestatistics');
    }

View file name: coursestatistics.blade.php file structure views/coursestatistics

Link to page:

<a class="navbar-brand" href="/courses/statistics">
   {{ __('Statistics') }}
</a>

Can anyone tell me what might be causing route not to work?

Sampan answered 16/4, 2018 at 7:15 Comment(5)
Show the complete route file content, one of your route's might be overriding this oneTypebar
Question updated with complete route fileSampan
Ok thanks, much betterTypebar
Fellow South African :P you spelled software wrong on your profile :DRollie
Haha well spotted thank you @Rollie will be sure to fix thatSampan
T
17

Try placing

Route::get('/courses/statistics', 'CourseController@statistics');

below this particular line of route code

Route::get('/courses/create', 'CourseController@create');

The general rule of laravel routing is to place specific routes before wildcard routes that are related. Link here

Typebar answered 16/4, 2018 at 7:24 Comment(5)
That worked for me thanks!, might have found another issue with Route::get('/progress/{section}', 'UserProgressController@show'); possibly because the wildcard is not named the progress as well causing issues with any routes below this one.Sampan
Ahh nice catch! :DRollie
@JellyBean Were you able to fix the progress route too?Typebar
yes just name the wild card the same as the route all is well thank youSampan
Update on my comment about the wild card, its not that it should be named the same as the route, it could be named anything as long as the wild card used is also used in the controller e.g. public function store(Model $wildcard){ ... }Sampan
D
23

I had same issue, Did all those magic with configs and nothing.. Solution: run: php artisan route:clear

Dedra answered 16/4, 2021 at 10:10 Comment(0)
T
17

Try placing

Route::get('/courses/statistics', 'CourseController@statistics');

below this particular line of route code

Route::get('/courses/create', 'CourseController@create');

The general rule of laravel routing is to place specific routes before wildcard routes that are related. Link here

Typebar answered 16/4, 2018 at 7:24 Comment(5)
That worked for me thanks!, might have found another issue with Route::get('/progress/{section}', 'UserProgressController@show'); possibly because the wildcard is not named the progress as well causing issues with any routes below this one.Sampan
Ahh nice catch! :DRollie
@JellyBean Were you able to fix the progress route too?Typebar
yes just name the wild card the same as the route all is well thank youSampan
Update on my comment about the wild card, its not that it should be named the same as the route, it could be named anything as long as the wild card used is also used in the controller e.g. public function store(Model $wildcard){ ... }Sampan
S
2

If issue remains same After cache clear or laravel routing rule, use 'composer dump-autoload'

Shaman answered 7/8, 2022 at 7:13 Comment(0)
K
0

If in your controller you have Model::findorFail($id) and an object with that ID does not exist, it can also lead to this error (in Laravel 6)

Kingery answered 19/8, 2022 at 7:53 Comment(0)
P
0

I have got the same problem with Laravel 9. In my case the problem was with htaccess file. You can have a look on your htaccess file.

Pneumo answered 24/6, 2024 at 16:39 Comment(1)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewListerism

© 2022 - 2025 — McMap. All rights reserved.