简体   繁体   English

在Laravel 5中,如何将控制器移出'app'文件夹

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

I am placing all the classes codes in a separate folder. 我将所有类代码放在单独的文件夹中。 I have created a folder 'src' alongside the folder 'app'. 我在文件夹“ app”旁边创建了一个文件夹“ src”。 Within this 'src' folder will be 'Models', 'Views' and 'Controllers' folder and 'routes.php' file too. 在此“ src”文件夹中还将包含“模型”,“视图”和“控制器”文件夹以及“ routes.php”文件。 So the scructure will be as follows. 因此,结构将如下。

  • app 应用
  • src SRC
    • Controllers 控制器
    • Models 楷模
    • Views 查看
    • routes.php 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'. 我已经通过更改“ app \\ Providers \\ RouteServiceProvider.php”中的路由路径和“ config / view.php”中的视图路径,将routes.php和视图移至src。 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? 我的问题是-如何将控制器,模型,视图和route.php从应用程序移动到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. 我不想移动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. 我想实现这一目标而不在其他地方刹车。

You can move your code there, you'll just need to tell autoloader where to find those classes. 您可以将代码移到那里,只需要告诉autoloader在哪里可以找到那些类。 You'll need to update autoload section in your composer.json . 您需要在composer.json中更新autoload部分。

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: 如果将它们存储在\\ App \\ Http \\ Controllers中 ,则需要在其中添加以下内容:

"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. 这将告诉自动加载器, App \\ Http \\ Controllers命名空间的根目录是src / Controllers /文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将我的控制器移动到Laravel 5.2中的单独包文件夹中? - How to move my controllers into a separate package folder in Laravel 5.2? 如何在Laravel 4的app文件夹之外制作控制器,模型,视图? - How to make controllers, models, views outside app folder in Laravel 4? 如何在 Laravel 中的 App\\Http\\Controllers 文件夹外使用控制器? - How to use controller outside folder App\Http\Controllers in Laravel? 如何解决错误:在laravel 5.3中调用未定义函数App \\ Http \\ Controllers \\ JSON_EXTRACT()? - How can I resolve error : Call to undefined function App\Http\Controllers\JSON_EXTRACT() in laravel 5.3? Laravel,如何使用另一个控制器到同一文件夹 - Laravel, how to use another controllers to the same folder 如何使用 id 获取 laravel 中控制器的 href - How can i get the href for controllers in laravel using id 如何在Laravel中使用Swoole正确路由和控制器? - How can I do right routes and controllers with swoole in laravel? 如何在 Laravel 控制器中使用 null 或为空? - How can I use null or empty in Laravel Controllers? Laravel:将上传文件夹移出 public_html 文件夹 - Laravel: Move the upload folder out of the public_html folder 如何在laravel 5.3中删除文件夹? - How can I delete folder in laravel 5.3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM