[英]Laravel package Controller not found in route
I have a simple package and I want to use the controller. 我有一个简单的程序包 ,我想使用控制器。 When I try to use it in routes I got
当我尝试在路线中使用它时
Class App\Http\Controllers\Tropicalista\Admin\Controllers\DashboardController
does not exist
I have this in my /routes/web.php 我在/routes/web.php中有这个
Route::group([
'namespace' => '\Tropicalista\Admin\Controllers',
'prefix'=> 'admin'], function() {
Route::get('/', ['as' => 'admin.root', 'uses' => 'DashboardController@index']);
});
My controller: 我的控制器:
namespace Tropicalista\Admin\Controllers;
use Illuminate\Http\Request;
use Analytics;
use Carbon\Carbon;
use Spatie\Analytics\Period;
use Illuminate\Support\Collection;
use Illuminate\Routing\Controller;
class DashboardController extends Controller
{...}
I think is a namespace problem. 我认为是名称空间问题。 So how can I call the package controller?
那么我怎么称呼包控制器呢?
By default, the
RouteServiceProvider
includes your route files within anamespace
group, allowing you to register controller routes without specifying the fullApp\\Http\\Controllers
namespace prefix.默认情况下,
RouteServiceProvider
将您的路由文件包括在namespace
组中,从而使您无需指定完整的App\\Http\\Controllers
命名空间前缀即可注册控制器路由。 So, youonly
need to specify the portion of the namespace thatcomes after
the baseApp\\Http\\Controllers
namespace.所以,你
only
需要指定命名空间的一部分comes after
基App\\Http\\Controllers
命名空间。
You need to remove namespace 您需要删除名称空间
Route::group(['prefix'=> 'admin'], function() {
Route::get('/', ['as' => 'admin.root', 'uses' => '\Tropicalista\Admin\Controllers\DashboardController@index']);
});
Since it's a package, you need to register the routes in the package. 由于是包裹,因此您需要在包裹中注册路线 。
You can see an example of registering package controllers here : 您可以在此处查看注册程序包控制器的示例:
$routeConfig = [
'namespace' => 'Barryvdh\Debugbar\Controllers',
'prefix' => $this->app['config']->get('debugbar.route_prefix'),
'domain' => $this->app['config']->get('debugbar.route_domain'),
'middleware' => [DebugbarEnabled::class],
];
$this->getRouter()->group($routeConfig, function($router) {
$router->get('open', [
'uses' => 'OpenHandlerController@handle',
'as' => 'debugbar.openhandler',
]);
});
In order to call package controller, change the namespace group of RouteServiceProvider from 为了调用程序包控制器,请从以下位置更改RouteServiceProvider的名称空间组:
protected $namespace = 'App\Http\Controllers';
to null/empty ie 为空/空即
protected $namespace = '';
Then, the route can be written as, 然后,路线可以写成
Route::get('homepage', 'Package\Namespace\Controllers\ControllerName@ActionName');
Further, if you want to write route for the default controller, use leading slash '/' before starting url. 此外,如果要为默认控制器写路由,请在开始url之前使用斜杠“ /”。
Route::get('/homepage', 'App\Http\Controllers\ControllerName@ActionName');
Whether it is good practice or not but it solved the problem. 不管是好的做法,还是解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.