【发布时间】:2018-05-05 22:20:46
【问题描述】:
我在不同于 Laravel 原生 App\Http\Controllers 的文件夹中有控制器。我正在使用一个自定义的Lib\MyApp 文件夹,里面有模块。每个模块都有自己的控制器、模型等。我添加到composer.json 自动加载到app\lib。
我所做的是更改 RouteServiceProvider 命名空间:
protected $namespace = 'App\Lib\MyApp';
我做了一个composer dump-autoload。
在MyApp 里面是一个Landing\Controller 文件夹,里面有实际的控制器类。
尝试 1(理想):
我想这样称呼我的路线:
Route::get('/', 'Landing\Controller\LandingController@index');
但是这样我得到了一个ReflectionException,即使没有找到该类
尝试 2:
Route::get('/', '\Landing\Controller\LandingController@index');
刷新页面时,斜杠去掉了命名空间部分,仍然说类不存在。
尝试 3:
Route::get('/', 'MyApp\Landing\Controller\LandingController@index');
这只是复制了MyApp 文件夹,并且没有按预期找到类。
尝试 4(工作,但不想那样)
Route::get('/', '\MyApp\Landing\Controller\LandingController@index');
这很好用,虽然我想去掉 \MyApp\ 部分。
这样的事情可能吗?
【问题讨论】:
-
这样的命名空间怎么样:
Route::namespace('Landing\Controller')->group(function () { Route::get('/', 'LandingController@index'); // + other routes in the same namespace });?? -
虽然我喜欢这种方法,但这种方法不能与任何组合一起使用
-
我认为它必须按照documentation工作:p
-
是的,但我不知道我做错了什么......可能是一些错误的斜线或其他东西。有趣的是,它给了我一个类不存在的错误,即使它抛出的路径是正确的......
-
您还为
LandingController命名为<?php namespace App\Lib\MyApp\Landing\Controller;吗??