• 2


    laravel version: 5.5.*
    路由文件: route/web.php

    路由类型

    • get 只匹配get类型的请求
    • post 只匹配post类型的请求
    • delete 只匹配delete类型的请求
    • put 只匹配put类型的请求
    • any 匹配 get post delete put 任意一种类型的请求

    基本使用

    基本格式: Route::路由方式('路由', '命名空间控制器@方法');

    • 在PHP中..
    // 基本路由
    Route::get('/', 'TestController@index');
    Route::post('/store', 'TestController@store');
    Route::put('/update/{id}', 'TestController@update');
    Route::delete('/delete/{id}', 'TestController@destroy');
    RouteW::any('/test', 'TestController@test');
    
    // 特殊路由
    Route::resource('/article', 'ArticleController');
    Route::resources([
        '/article' => 'ArticleController',
        '/comment' => 'CommentController',
    ]);
    
    • 在模板中..
    <!doctype html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>laravel route</title>
    </head>
    <body>
        <!-- Get Request -->
        <a href="/">Get Request</a>
    
        <!-- Post Request -->
        <form action="/store" method="post">
            {{csrf_filed()}}
            <button>Post Request</button>
        </form>
    
        <!-- Put Request -->
        <form action="/put/1" method="post">
            {{csrf_filed()}}
            @method('PUT')
            <button>Put Request</button>
        </form>
    
        <!-- Delete Request -->
        <form action="/put/1" method="post">
            {{csrf_filed()}}
            @method('DELETE')
            <button>Delete Request</button>
        </form>
    
        <!-- 除了使用框架的函数和模板引擎, 也可以使用这样的方式来访问 -->
        <form action="/put/1" method="post">
            <input type="hidden" name="_method" value="PUT">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <button>Delete Request</button>
        </form>
    </body>
    <html>
    

    CSRF 验证

    所有 POST 类型的路由(包括 _POST_,_PUT_, _DELETE_) , 都必须使用通过 CSRF 验证才能匹配到路由

    {{csrf_filed()}} // 生成 scrf token 字段 
    {{csrf_token()}} // 生成 scrf token 值
    

    路由参数

    • 必须参数: 访问路由时, 没有指定的参数就不能访问
    Route::get('user/{id}', function ($id) {
        return 'User ' . $id;
    });
    
    • 可选参数: 访问路由时, 没有指定的参数也能访问
    Route::get('user/{id?}', function ($id) {
        $userId = $id ? $id : '';
        return 'User ' . $userId;
    });
    
    • 多个参数
    Route::get('user/{id}/{name?}', function ($id, $name='') {
        return $name .'-'. $id;
    });
    
    • 参数约束, get请求 的参数能够在地址栏中修改,为了保证参数的正确性,可以使用参数约束
    // 约束单个参数
    Route::get('user/{id}', function ($id) {
        // $id 必须是数字,且不能为空
        return "hello world";
    })->where('id', '[0-9]+');
    
    // 约束多个参数
    Route::get('user/{id}/{name}', function ($id) {
        // $id 必须是数字,且不能为空
        return "hello world";
    })->where(['id'   => '[0-9]+', 'name' => 'w{3,5}']);
    

    路由重定向

    访问第一个路由跳转到第二个路由

    Route::redirect('第一个路由', '第二个路由', HTTP状态码);

    Route::redirect('/here', '/there', 301);
    

    路由命名

    命名路由为生成 URL 或重定向提供了方便

    Route::get('user/profile', function () {
        // 使用 route 助手函数, 通过路由名称生成 URL
        return "url". route('profile');
    })->name('profile');
    
    // 也可以这样
    Route::get('user/profile', 'UsersController@profile')->name('profile');
    

    路由分组

    使用路由分组,可以方便的设置多个路由的 中间件 命令空间 前缀 等相同的信息

    Route::group([
        'middleware' => 'login', // 如果有多个可以使用数组 ['login', 'admin']
        'namespace'  => 'admin',
        'prefix'     => '/admin',
    ], function () {
        Route::get('/uers/index', 'UsersController@index');
        // 等价于: Route::get('/admin/uers/index','adminUsersController@index')->middleware('login');
    });
    

    资源路由

    资源路由一般会和资源控制器配合使用

    使用资源路由,可以快速生成多条常用的路由, 多资源路由的原理也是一样

    // 资源路由
    Route::resource('/admin/user', 'adminUsersController');
    
    // 批量资源路由
    Route::resource([
        '/admin/user' => 'adminUsersController',
        '/admin/article' => 'adminArticlesController',
    ]);
    
    7942449-e156dde812841988.png
    资源路由

    自定义错误页面

    • resource/views 目录下新建一个 errors 目录
    • 自定义指定 http 状态码blade模板文件
    • abort() 中断 request请求
    Route::get('/404', function () {
        abort(404, 'Not Found'); // 404.blade.php
        abort(503); // 503.blade.php
    });
    
  • 相关阅读:
    MongoDB数据库新建数据库用户
    Grafana部署
    k8s ingress及ingress controller
    Rabbitmq如何安装插件
    RabbitMQ手册之rabbitmq-plugins
    RabbitMQ运行在Docker容器中
    K8S资源限制
    System类
    Runtime类
    StringBuffer类
  • 原文地址:https://www.cnblogs.com/liaohui5/p/10581618.html
Copyright © 2020-2023  润新知