4

I am building a module called Student in Laravel.

I use the routes.php file inside the Student folder to write routes realted to student module..

When I use just Route::get('/list', function () { return view('welcome');}); program working fine without error.

But when I am using Route::get('/list', 'StudentController@list'); there is a error.

Error is,

Class App\Http\Controllers\StudentController does not exist

Folder Structure

enter image description here

Student Controller

namespace App\Student\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class StudentController extends Controller { public function list(){ echo "Hello" } } 

Student Service Provider

namespace App\Student; use App\Providers\RouteServiceProvider as ServiceProvider; use Illuminate\Support\Facades\Route; class StudentServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { parent::boot(); } /** * Register the application services. * * @return void */ public function register() { // } /** * Define the routes for the application. * * @internal param Router $router */ public function map() { Route::group([ 'namespace' => $this->namespace, 'prefix' => 'students', ], function ($router) { require __DIR__ . '/routes.php'; }); } } 
4
  • try with this Route::get('student/list', 'StudentController@list'); Commented Sep 6, 2017 at 7:47
  • Can you show the content of App\Providers\RouteServiceProvider and routes.php ? Commented Sep 6, 2017 at 8:02
  • @Kaspars It is just Route::get('/list', 'StudentController@list'); line in students/routes.php. I have not touched RouteServiceProvider file. github.com/laravel/laravel/blob/master/app/Providers/… Commented Sep 6, 2017 at 8:06
  • Check this link out: stackoverflow.com/a/63871723/5857099 Commented Sep 13, 2020 at 14:14

7 Answers 7

9

Although laravel is magic at times, it only works if you stick the the default configuration and conventions.

You can place your controllers anywhere (heck, even load from a database and eval them) but you have to change the configuration accordingly.

I suspect you have the wrong namespace configured in RouteServiceProvider. By default it is App\Http\Controllers.

Changing default folder

If all your controllers will be in the same folder, change it to App\Student\Controllers and forget about it.

class RouteServiceProvider extends ServiceProvider { // ... protected $namespace = 'App\Student\Controllers'; // ... } 

Multiple modules

If you want to have multiple modules, then change your RotueServiceProvider namespace config to App and in route files use Student\Controllers\StudentController@list

class RouteServiceProvider extends ServiceProvider { // ... protected $namespace = 'App'; // ... } Route::get('/list', 'Student\Controllers\StudentController@list'); 
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer worked perfectly fine. And I got another answer from another forum. It is just adding protected $namespace = 'App\Student\Controllers'; to Student Service Provider page. It also worked fine.
4

go to RouteServiceProvider.php and the changed the namespace to default

protected $namespace = 'App\Http\Controllers'; 

Comments

2

it gives you that error because the controller couldn't find the class you are calling .. in the top of your controller add

use App\Student; 

to make it work ..

5 Comments

Thanks for the answer. I tried it also. But same result.
oh my bad i thought the model was the missing .. updating my answer
No, still I haven't used models for this project. Do you have any idea to solve this issue?
the route couldn't find the controller .. have you tried Route::get('/list', 'Student/Controllers/StudentController@list'); or Route::get('/list', 'Controllers/StudentController@list');
I can't move it to HTTP folder because I must use this strcuture for this project. I tried Route::get('/list', 'Student/Controllers/StudentController@list'); also. But same error. ? :-(
1

You create controller in wrong location. Default Controller location is :

app/Http/Controllers 

2 Comments

actually he may do that .. he had his own coding convention .. he had change its namespace as you can see .. what he's missing is the use App\Student
No, when you create modules you can create folder called 'controllers' under another folder and create controllers.
0

Your controller is in wrong place. It should be inside app/Http/Controllers/.

You can make further folder Student inside app/Http/Controllers/ and extending the main Controller to make your isolated module Student.

You file structure should like:

app[dir] ----Http[dir] -------Controllers[dir] ----------Student[dir] ----------Controller.php[file] 

Your Student Controller should look like,

namespace App\Http\Controllers\Student; use App\Http\Controllers\Controller; class StudentController extends Controller { public function __construct() { $this->middleware('auth'); } .... } 

4 Comments

No, when you create modules you can create folder called 'controllers' under another folder and create controllers
@IamtheMostStupidPerson I've used above approach for such situation. This may or mayn't acceptable for you.
when you working big projects you may have to create modules. Read more : stackoverflow.com/a/39846287/8246224
@IamtheMostStupidPerson I agree with you.
0

Try to rewrite your route by this:

 Route::get('/list', '\App\Student\Controllers\StudentController@list'); 

Hope it help you :)

Comments

0

The same problem occurs when you move a previously created Controller into another folder inside Controllers folder:

|Controllers |Controller.php |--|Setup [Folder] |MovedController.php 

Just make sure the namespace is correct and import Controller.php

namespace App\Http\Controllers\Setup; use App\Http\Controllers\Controller; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.