I a*_*son 2 php laravel laravel-routing laravel-5 laravel-controller
我正在Laravel中建立一个名为Student的模块。
我使用Student文件夹中的routes.php文件来编写路由到Student模块的路由。
当我使用Route::get('/list', function () { return view('welcome');});程序时,程序运行正常,没有错误。
但是当我使用时Route::get('/list', 'StudentController@list');出现错误。
错误是
类App \ Http \ Controllers \ StudentController不存在
资料夹结构
学生主管
namespace App\Student\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class StudentController extends Controller
{
public function list(){
echo "Hello"
}
}
Run Code Online (Sandbox Code Playgroud)
学生服务提供者
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';
});
}
}
Run Code Online (Sandbox Code Playgroud)
尽管laravel有时很神奇,但是只有遵循默认配置和约定,它才有效。
您可以将控制器放置在任何地方(瓶颈,甚至从数据库及其加载eval),但必须相应地更改配置。
我怀疑您在RouteServiceProvider中配置了错误的名称空间。默认情况下为App\Http\Controllers。
如果您所有的控制器都在同一个文件夹中,请将其更改为App\Student\Controllers并忽略它。
class RouteServiceProvider extends ServiceProvider
{
// ...
protected $namespace = 'App\Student\Controllers';
// ...
}
Run Code Online (Sandbox Code Playgroud)
如果要有多个模块,则将RotueServiceProvider命名空间配置更改为App路由文件并在其中使用Student\Controllers\StudentController@list
class RouteServiceProvider extends ServiceProvider
{
// ...
protected $namespace = 'App';
// ...
}
Route::get('/list', 'Student\Controllers\StudentController@list');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15033 次 |
| 最近记录: |