PHP

laravel路由笔记

是滑稽啊
2020-07-17 / 0 评论 / 105 阅读 / 正在检测是否收录...
  1. 线上正式生产环境
  2. 测试环境(局域网内,模拟线上环境)
  3. 开发环境(本地开发)

facades(门面xx) 佛萨斯

1.通过路由访问 (控制器,视图,闭包,重定向地址)

接口降低耦合度

容器-目的:不重复造轮子

使用指令创建

php artisan make:controller HelloController//创建控制器
php artisan make:model Order//创建模型
php artisan make:event OrderPaid//创建事件

中间件在路由和控制之间对接受的字段进行处理。

测试路由post时候发现错误

注意CSRF 保护

不需要csrf保护,在控制器中VerifyCsrfToken.php中接入http完整访问路径,可绕过保护。

//生成URLecho $url=route('profile');

//生成重定向return redirect()->route('profile');

普通路由简单的访问配置

Route::get("路由名",“控制器名@方法”);
//访问视图(闭包)
Route::get('hello', function () {
    return view('hello');
});
//访问视图
Route::view('hello','hello');
//访问控制器
Route::any('index','IndexController@index');
Route::any('user','user\IndexController@index');
//使用闭包
Route::match(['get','post'],'blog/{id}/{name?}',function($id,$name=''){
    return 'blog'.$id.'-'.$name;
    });

路由参数:必填,选填,正则表达式约束,全局属性

//正则约束
Route::any('user/{id?}','IndexController@index')->where(['id'=>'[0-5]{2}']);

全局变量在http/Providers/RouteServiceProvider.php

public function boot(){
    Route::pattern('id','[0-9]+');
    parent::boot();
}

特殊路由:

重定向路由

Route::redirect('/here','/there',301);//参数3默认是302

视图路由

Route::view('hello','hello');//参数3是[路由参数]

推荐使用any ,不推荐match(在接口请求用的多post,get,put)

路由起名

//往起名的路由中传参
Route::get('test', function () {
    echo route("yonghu",['id'=>11,'name'=>'bilibili']);
    echo "<br/>";
    echo route("blog",['id'=>12,'name'=>'acfun']);
});
//起名
Route::any('user/{id?}','IndexController@index')
    ->where(['id'=>'[0-5]{2}'])
    ->name('yonghu');
//起名
Route::match(['get','post'],'blog/{id}/{name?}',function($id,$name=''){
    return 'blog'.$id.'-'.$name;
})->name('blog');

检查路由

路由中间件

定义中间件指令:php artisan make:middleware CheckAge

手动建立

<?php

namespace App\Http\Middleware;

use Closure;

class CheckAge
{
    public function handle($request, Closure $next)
    {
        if ($request->age <= 200) {
            return redirect('home');
        }

        return $next($request);
    }
}

中间件定义又分为:前置/后置中间件

中间件是在请求之前或之后执行,取决于中间件本身

// 请求之前执行:执行一些任务 return $next($request);

//请求之后执行:$response = $next($request); // 执行一些任务return $response;

在ChekAge中填写定义任务,比如:

public function handle($request, Closure $next)
    {
        if ($request->age <= 200) {//如果age<200使用hello路由
            return redirect('hello');
        }

        return $next($request);//否则只用本条路由
    }

在Http/Kernel.php中添加中间件定义在$routeMiddleware变量中

'check' => \App\Http\Middleware\CheckAge::class,

最后在路由中使用middleware

Route::any('index','IndexController@index')
    ->middleware('\App\Http\Middleware\CheckAge::class');
//或者使用上面routeMiddleware中自定义check
//->middleware('check');
//或者使用use(第二行代码简写)
//use App\Http\Middleware\CheckAge;
//->middleware(CheckAge::class);

访问:http://localhost/index?age=201跳转index路由,如果age=200跳转hello路由

路由中间件传参

//中间件CheckAge方法
public function handle($request, Closure $next,$test)
    {
        echo $test.'<br/>';
        if ($request->age <= $test) {
            return redirect('hello');
        }
        return $next($request);
    }
//web路由
Route::any('index','IndexController@index')->middleware('check:100');

访问index?age=99,走判断里的hello路由。

访问index?age=101,走本身index路由

terminable最终中间件-详情见管方手册

分组路由

Route::middleware('check:100,xiao')//路由参数设置
        ->domain('{goods}.laravel.cn')//子域名设置
        ->name('admin.')//路由名前缀设置
        ->prefix('zx')//路由前缀设置
        ->namespace('User')//命名空间设置
        ->group(function(){
    Route::any('user/{id?}','IndexController@index')
        ->where(['id'=>'[0-5]{2}'])
        ->name('yonghu');
    Route::match(['get','post'],'blog/{id}/{name?}',function($goods,$id,$name=''){
        return 'blog'.$id.'-'.$name.'子域名:'.$goods;
    })->name('blog');
});

回退路由

Route::fallback(function(){
    echo '404';
});
0

评论

博主关闭了所有页面的评论