我尝试使用路由在Laravel 5.2中创建包。我在Http/Controllers
文件夹中有控制器(名称空间为vendor\package\Http\Controllers\MyController
)。我想创建别名,但我不知道如何。我不想在routes.php
中调用此控制器:
Route::get('myurl', vendor\package\Http\Controllers\MyController::class . '@action');
但是那样:
Route::get('myurl', 'MyController@action');
我尝试在Application
类API中搜索此内容,但无法找到此信息。
此代码包提供程序无效。
$this->app
->alias(vendor\package\Http\Controllers\MyController::class, 'MyController');
我的服务提供商:
class PackageRouterServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
include(__DIR__ . '/../Http/routes.php');
$this->app->alias(MyController::class, 'MyController'); // <- I tried this
}
}
答案 0 :(得分:1)
尝试像这样更新PackageRouterServiceProvider.php
:
class PackageRouterServiceProvider extends ServiceProvider
{
// Set this namespace to your package controllers namespace.
protected $namespace = 'vendor\package\Http\Controllers';
public function boot(Router $router)
{
//
}
public function register()
{
$this->mapRoutes($this->app->router);
}
protected function mapRoutes(Router $router)
{
$router->group(['namespace' => $this->namespace], function($router) {
require (__DIR__ . '/../Http/routes.php');
});
}
}
当然,您需要在Laravel config app.php
文件中注册此提供程序才能工作。