2018-01-24 06:20
浏览 218


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

    '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?

图片转代码服务由CSDN问答提供 功能建议

我有简单的包,我想使用控制器。 当我尝试在路由中使用它

  Class App \ Http \ Controllers \ Tropicalista \ Admin \ Controllers \ DashboardController 


 路径::组([\''命名空间'=>'\  Tropicalista \ Admin \ Controllers',
 Route :: get('/',['as'=>'admin.root',  'uses'=>'DashboardController @ index']); 

我的控制器: < pre> 命名空间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扩展Controller {...}

我认为是命名空间问题。 那么如何调用包控制器呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglei7152
    donglei7152 2018-01-24 06:33

    By default, the RouteServiceProvider includes your route files within a namespace group, allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix. So, you only need to specify the portion of the namespace that comes after the base App\Http\Controllers namespace.

    You need to remove namespace

    Route::group(['prefix'=> 'admin'], function() {
        Route::get('/', ['as' => 'admin.root', 'uses' => '\Tropicalista\Admin\Controllers\DashboardController@index']);
    点赞 评论
  • dousi1970
    dousi1970 2018-01-24 06:40

    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',
    点赞 评论
  • doujiu7680
    doujiu7680 2019-03-22 04:53

    In order to call package controller, change the namespace group of RouteServiceProvider from

    protected $namespace = 'App\Http\Controllers';

    to null/empty i.e.

    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.

    Route::get('/homepage', 'App\Http\Controllers\ControllerName@ActionName');

    Whether it is good practice or not but it solved the problem.

    点赞 评论