douniangliao4327
douniangliao4327
2018-01-24 06:20
浏览 218
已采纳

Laravel包控制器在路线中找不到

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

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?

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

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

  Class App \ Http \ Controllers \ Tropicalista \ Admin \ Controllers \ DashboardController 
does不存在
   
 
 

我在/routes/web.php中找到了这个

 路径::组([\''命名空间'=>'\  Tropicalista \ Admin \ Controllers',
'前缀'=>'admin'],function(){
 
 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.

    点赞 评论

相关推荐