好吧,所以我一直试图改变我的控制器路径(所以我的项目更结构化了),但遗憾的是,每次我尝试使用控制器进行路由时,都会这样:
Route::get('/', ['as' => 'home', 'uses' => 'PagesController@index']);现在返回一个错误页面:
ReflectionException
Class PagesController does not exist因此,我认为让我自动添加psr-4并/或将其添加到类图中,这样我的composer.json就是这样的:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*",
"cartalyst/sentinel": "dev-master",
"guzzlehttp/guzzle": "4.*",
"way/generators": "2.*"
},
"repositories": [
{
"type": "composer",
"url": "http://packages.cartalyst.com"
}
],
"autoload": {
"classmap": [
"app/database/migrations",
"app/database/seeds",
"app/strifemods/Controllers",
"app/strifemods/Models"
],
"psr-4": {
"Strifemods\\": "app/Strifemods"
}
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "dev",
"prefer-stable": true}
之后,它仍然无法工作,所以我决定在我亲爱的/卖主/composer/目录中查看它是否实际被加载。
// autoload_classmap.php @generated by Composer
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'CreateSessionTable' => $baseDir . '/app/database/migrations/2014_07_06_213148_create_session_table.php',
'DatabaseSeeder' => $baseDir . '/app/database/seeds/DatabaseSeeder.php',
'IlluminateQueueClosure' => $vendorDir . '/laravel/framework/src/Illuminate/Queue/IlluminateQueueClosure.php',
'MigrationCartalystSentinelAlterThrottle' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_025024_migration_cartalyst_sentinel_alter_throttle.php',
'MigrationCartalystSentinelAlterUsers' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_023520_migration_cartalyst_sentinel_alter_users.php',
'MigrationCartalystSentinelInstallActivations' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_014954_migration_cartalyst_sentinel_install_activations.php',
'MigrationCartalystSentinelInstallGroups' => $vendorDir . '/cartalyst/sentinel/src/migrations/2012_12_06_225929_migration_cartalyst_sentinel_install_groups.php',
'MigrationCartalystSentinelInstallReminders' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_022418_migration_cartalyst_sentinel_install_reminders.php',
'MigrationCartalystSentinelInstallThrottle' => $vendorDir . '/cartalyst/sentinel/src/migrations/2012_12_06_225988_migration_cartalyst_sentinel_install_throttle.php',
'MigrationCartalystSentinelInstallUsers' => $vendorDir . '/cartalyst/sentinel/src/migrations/2012_12_06_225921_migration_cartalyst_sentinel_install_users.php',
'MigrationCartalystSentinelInstallUsersGroupsPivot' => $vendorDir . '/cartalyst/sentinel/src/migrations/2012_12_06_225945_migration_cartalyst_sentinel_install_users_groups_pivot.php',
'MigrationCartalystSentinelRenameAlterGroups' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_023945_migration_cartalyst_sentinel_rename_alter_groups.php',
'MigrationCartalystSentinelRenameAlterGroupsUsersPivot' => $vendorDir . '/cartalyst/sentinel/src/migrations/2013_11_26_024557_migration_cartalyst_sentinel_rename_alter_groups_users_pivot.php',
'SessionHandlerInterface' => $vendorDir . '/symfony/http-foundation/Symfony/Component/HttpFoundation/Resources/stubs/SessionHandlerInterface.php',
'Strifemods\\Controllers\\BaseController' => $baseDir . '/app/strifemods/Controllers/BaseController.php',
'Strifemods\\Controllers\\PagesController' => $baseDir . '/app/strifemods/Controllers/PagesController.php',
'Whoops\\Module' => $vendorDir . '/filp/whoops/src/deprecated/Zend/Module.php',
'Whoops\\Provider\\Zend\\ExceptionStrategy' => $vendorDir . '/filp/whoops/src/deprecated /Zend/ExceptionStrategy.php',
'Whoops\\Provider\\Zend\\RouteNotFoundStrategy' => $vendorDir . '/filp/whoops/src/deprecated/Zend/RouteNotFoundStrategy.php',
);而且它正在加载,但它找不到类,谁能对此有所了解并帮我解决这个问题;)
发布于 2014-07-07 23:02:09
这个错误在技术上是正确的。尽管PagesController存在,但它只存在于某个地方有一个具有该名称的类中。但是,由于类是命名空间的,因此应该将其称为Strifemods\Controllers\PagesController,但在Strifemods\Controllers上下文中除外。
以您的路线为例,您可以这样做:
Route::get('/', [
'as' => 'home',
'uses' => 'Strifemods\Controllers\PagesController@index'
]);如果控制器中有子名称空间,比如API和Admin,您可以执行以下操作:
Route::group(['namespace' => 'Strifemods\Controllers\API'], function()
{
Route::get('/', [
'as' => 'api.home',
'uses' => 'PagesController@index'
]);
});
Route::group(['namespace' => 'Strifemods\Controllers\Admin'], function()
{
Route::get('/', [
'as' => 'admin.home',
'uses' => 'PagesController@index'
]);
});这允许您拥有Strifemods\Controllers\API\PagesController和Strifemods\Controllers\Admin\PagesController。希望这能有所帮助。
发布于 2019-01-02 22:47:08
这就是如何使您的项目更加结构化:
每个路由文件(web.php,api.php .)在map()方法中在文件中声明
\app\Providers\RouteServiceProvider.php当您映射一个路由文件时,您可以为它设置一个->namespace($this->namespace),您将在示例中看到它。
这意味着您可以创建更多的文件,使您的项目更加结构化!
并为每个名称空间设置不同的名称空间。
但我更喜欢将empty string设置为命名空间""
它让您以本机php的方式将控制器设置为rout,参见示例:
Route::resource('/users', UserController::class);
Route::get('/agents', [AgentController::class, 'list'])->name('agents.list');现在,您可以双击IDE中的控制器名称,以便快速方便地到达那里。
https://stackoverflow.com/questions/24621139
复制相似问题