更改控制器路径
Posted
技术标签:
【中文标题】更改控制器路径【英文标题】:Changing controller path 【发布时间】:2014-08-28 13:23:37 【问题描述】:好吧,所以我一直在尝试更改我的控制器路径(所以我的项目更加结构化),但遗憾的是每次我尝试像这样使用控制器进行路由:
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
在那之后它仍然无法正常工作,所以我决定查看我亲爱的 /vendor/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',
);
它正在加载,但找不到类,任何人都可以对此有所了解并帮助我;)会应用它。
【问题讨论】:
Laravel Controller Subfolder routing的可能重复 【参考方案1】:这就是您可以使您的项目更有条理的方法:
在map()
方法中声明的每个路由文件(web.php
、api.php
...)在文件中
\app\Providers\RouteServiceProvider.php
当你映射一个路由文件时,你可以为它设置一个->namespace($this->namespace)
,你会在例子中看到它。
这意味着你可以创建更多的文件来让你的项目更有条理!
并为每一个设置不同的命名空间。
但我更喜欢为命名空间 ""
设置 empty string
它可以让你将控制器设置为以原生 php 方式路由,请参见示例:
Route::resource('/users', UserController::class);
Route::get('/agents', [AgentController::class, 'list'])->name('agents.list');
现在您可以在 IDE 中双击控制器名称,以便快速方便地到达那里。
【讨论】:
【参考方案2】:该错误在技术上是正确的。虽然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
。希望对您有所帮助。
【讨论】:
以上是关于更改控制器路径的主要内容,如果未能解决你的问题,请参考以下文章
Spring Data REST 基本路径的更改不涉及自定义控制器
需要更改 Laravel 控制器路径已在 serviceprovider 上设置命名空间为空