Laravel控制器不存在

发布于 2025-02-11 05:55:19 字数 725 浏览 0 评论 0原文

我想重定向到我的控制器而不是视图,但它说:“目标类[app/http/controllers/myFirstController]不存在。”

这是代码(web.php file):

//just a view
Route::get('/', function () {
    return view('index');
});

//just a view
Route::get('/final', function () {
    return view('welcome');
});

//the controller is interested in
Route::get('/hello-controller', 'app/Http/Controllers/MyFirstController@index');

controller code(app/http/controllers) /myfirstcontroller.php):

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyFirstController extends Controller
{
    public function index(){
        return "viva";
    }
}

其他信息: Laravel框架版本:8.83.17 PHP版本:PHP 7.4.29

I wanna redirect to my controller instead of a view but it says: "Target class [app/Http/Controllers/MyFirstController] does not exist. "

here is the code (web.php file):

//just a view
Route::get('/', function () {
    return view('index');
});

//just a view
Route::get('/final', function () {
    return view('welcome');
});

//the controller is interested in
Route::get('/hello-controller', 'app/Http/Controllers/MyFirstController@index');

Controller code (app/Http/controllers/MyFirstController.php) :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyFirstController extends Controller
{
    public function index(){
        return "viva";
    }
}

additional information:
Laravel Framework version: 8.83.17
PHP version : PHP 7.4.29

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

为你拒绝所有暧昧 2025-02-18 05:55:19

名称空间是不正确的:资本A用于应用程序,并使用\而不是/

Route::get('/hello-controller', 'App\Http\Controllers\MyFirstController@index');

,甚至更好:

Route::get('/hello-controller', [\App\Http\Controllers\MyFirstController::class, 'index']);

The namespace is not correct: Capital A for App and use \ instead of /

Route::get('/hello-controller', 'App\Http\Controllers\MyFirstController@index');

or even better :

Route::get('/hello-controller', [\App\Http\Controllers\MyFirstController::class, 'index']);
南笙 2025-02-18 05:55:19

尝试通过使用来清除控制器的缓存

php artisan route:cache

,也许您应该使用

//the controller is interested in
Route::get('/hello-controller', 'App/Http/Controllers/MyFirstController@index');

try to clear cache your controller by using

php artisan route:cache

and also maybe u should use

//the controller is interested in
Route::get('/hello-controller', 'App/Http/Controllers/MyFirstController@index');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文