好吧所以我一直试图改变我的控制器路径(所以我的项目更结构化)但遗憾的是每当我尝试使用控制器进行路由时这样:
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',
);
它正在加载,但它无法找到课程,任何人都可以对此有所了解并帮助我;)会对它进行评价。
答案 0 :(得分:1)
错误在技术上是正确的。虽然存在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
。希望有所帮助。
答案 1 :(得分:0)
这就是使项目更结构化的方法:
以web.php
方法声明的每个路由文件(api.php
,map()
...)在文件中
\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中双击您的控制器名称,以快速,方便地到达那里。