laravel8.x:运行起来(路由+控制器)

2023-10-16 16:11:30 浏览数 (2)

控制器生成命令:

代码语言:javascript复制
php artisan make:controller UserController

生成User控制器

控制器(UserController.php)

代码语言:javascript复制
<?php

namespace AppHttpControllers;//命名空间

use IlluminateHttpRequest;//请求类

class UserController extends Controller
{
    //

    public function Test()//方法
    {
        return 666;
    }
}

路由routes/web.php:

代码语言:javascript复制
  <?php

use IlluminateSupportFacadesRoute;
use AppHttpControllersUserController;//路由文件里面引入控制器,不然无法实现哦
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});
Route::get('Controller',[UserController::class,'Test']);
//意思就是在浏览器上面写Controller代表userController控制器的Test方法

需要注意的地方:

routes/web.php路由那块:要操作的是哪一个控制器得引入use哦.

效果:

0 人点赞