In Laravel 5, how can I move the controllers out of the 'app' folder

- 1 answer

Ad

I am placing all the classes codes in a separate folder. I have created a folder 'src' alongside the folder 'app'. Within this 'src' folder will be 'Models', 'Views' and 'Controllers' folder and 'routes.php' file too. So the scructure will be as follows.

  • app
  • src
    • Controllers
    • Models
    • Views
    • routes.php

I have moved the routes.php and views to src by changing the routing path in 'app\Providers\RouteServiceProvider.php' and view path in 'config/view.php'. Please correct me if I did anything wrong here.

My question is - how can I move controllers, models, views and route.php from app to src?

I want to do this because I want to palce all the user written codes in one place. I don't want to move the app/Http/Controllers/Controller.php. I just want to move the user created controllers.

I want to achive this without braking anything at other places.

Ad

Answer

Ad

You can move your code there, you'll just need to tell autoloader where to find those classes. You'll need to update autoload section in your composer.json.

What namespace are your controllers in? It will impact what you need to put there. If you store them in \App\Http\Controllers, you'll need to add the following there:

"autoload": {
  "classmap": [],
  "psr-4": {
    "App\\Http\\Controllers\\": "src/Controllers/",
  }
},

This will tell autoloader that the root directory for App\Http\Controllers namespace is src/Controllers/ folder.

Ad
source: stackoverflow.com
Ad