php Laravel路由器的URL是否正确?

h6my8fg2  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(74)

欢迎控制器

<?php

namespace App\Http\Controllers;

class WelcomeController extends Controller
{
    //
    public function index()
    {
        return view('welcome');
    }
}

ArticlesController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ArticlesController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return __METHOD__ . '은(는) Article 컬렉션을 조회합니다.';
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return __METHOD__ . '은(는)Article 컬렉션을 만들기 위한 폼을 담은 뷰를 반환합니다.';
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        return __METHOD__ . '은(는) 사용자의 입력한 폼 데이터로 새로운 Article 컬렉션을 만듭니다.';
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        return __METHOD__ . '은(는)다음 기본 키를 가진 Article 모델을 조회합니다.:'.$id;
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        return __METHOD__ . '은(는)다음 기본 키를 가진 Article 모델을 수정하기 위한 폼을 담은 뷰를 반환합니다.:'.$id;
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        return __METHOD__ . '은(는)사용자의 입력한 폼 데이터로 다음 기본 키를 가진 Article 모델을 수정합니다.:'.$id;
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        return __METHOD__ . '은(는)다음 기본 키를 가진 Article 모델을 삭제합니다.:'.$id;
    }
}

1.在VisualStudio代码控制台中输入PHP Artisan Make:Controller命令
1.创建WelcomeController和ArticlesController文件
web.app

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

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

Route::get('/', 'WelcomeController@index');

Route::resource('articles','ArticlesController');
  • 输入php artisan route:list命令x1c 0d1x

我在我的PHP Laravel项目的Visual Studio代码控制台中输入了命令php artisan route:list,但发生了错误。路径似乎是问题所在,我检查了app/Http/Controllers来匹配,但它没有解决它。

0tdrvxhp

0tdrvxhp1#

如果您使用的是Laravel 8或8+,请像这样编辑您的web.php文件,因为这是从laravel 8调用路由中的控制器类的现代方式。

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WelcomeController;
use App\Http\Controllers\ArticlesController; 

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

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

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

Route::resource('articles',ArticlesController::class);

并确保您的命名空间存在于您的RouteServiceProvider中,如下所示,

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

相关问题