【问题标题】:Unidentified Controller when using a route that calling a controller使用调用控制器的路由时未识别的控制器
【发布时间】:2021-12-23 21:17:04
【问题描述】:

美好的一天,感谢您阅读此问题

我有一个问题,我使用了不同的参数,但它不起作用,这是问题代码

Route::get('/profiles','ProfilesController@index');

但是当我使用这段代码时,它工作得非常好

Route::get('/profiles',[ProfilesController::class, 'index']);

这是控制器

class ProfilesController extends Controller
{
    public function index()
    {
        return profiles::all();
    }
}

【问题讨论】:

    标签: laravel routes controller


    【解决方案1】:

    你需要使用完整的命名空间App\Http\Controllers\ProfilesController@index

    use App\Http\Controllers\ProfilesController;
    
    // Using PHP callable syntax...
    Route::get('/profiles', [ProfilesController::class, 'index']);
    
    // Using string syntax...
    Route::get('/profiles', 'App\Http\Controllers\ProfilesController@index');
    

    如果您想继续使用原始的自动前缀控制器路由,您可以简单地在您的RouteServiceProvider 中设置$namespace 属性的值,并在boot 方法中更新路由注册以使用@ 987654327@财产。

    更多信息:

    https://laravel.com/docs/8.x/upgrade#automatic-controller-namespace-prefixing

    【讨论】:

    • 效果很好,非常感谢
    • @danielleander 不要忘记接受正确答案。
    猜你喜欢
    • 2015-03-12
    • 2017-02-10
    • 2016-08-06
    • 2014-12-15
    • 2014-03-05
    • 1970-01-01
    • 2018-06-10
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多