1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| <?php //最简单的路由 Route::get('/', function () { return view('welcome'); }); //hello请求的路由 Route::get('/hello', function () { return 'hello'; }); /*表单*/ //访问testPost生成一个表单,其中表单的action指向form_test,并且是POST请求 Route::get('/testPost', function () { $csrf_token = csrf_token(); $form = <<<FORM <form action="form_test" method="POST"> <input type="hidden" name="_token" value="{$csrf_token}"> <input type="submit" value="Test"/> </form> FORM; return $form; }); //接收表单的请求 Route::post('form_test', function () { return "表单POST测试"; }); /*表单*/ //路由通过match来匹配method Route::match(['get', 'post'], '/match', function () { return "路由Match方法"; }); //路由通过any来匹配任意请求 Route::any('/any', function () { return "路由Any方法"; }); //设置路由参数,如果有默认值一定要使用{name?} Route::get('/hello1/{name?}', function ($name = "haha") { return "Hello {$name}!"; }); //通过where设置路由条件,这里接收name参数,但是如果where里面的条件不满足,则会抛出异常 Route::get('/hello2/{name?}', function ($name = "Laravel") { return "Hello {$name}!"; })->where('name', '[A-Za-z]+'); //通过as关键字给路由命名 Route::get('/hello/laravelacademy', ['as' => 'academy', function () { return 'Hello LaravelAcademy!'; }]); //跳转到名字叫academy的路由 Route::get('/testNamedRoute', function () { return redirect()->route('academy'); }); //给路由组设置名称前缀 Route::group(['as' => 'admin::'], function () { Route::get('dashboard', ['as' => 'dashboard', function () { return "admin"; }]); }); //跳转到名叫admin的路由组里面的dashboard路由 Route::get('/testNamedRoute1', function () { return redirect()->route('admin::dashboard'); }); //设置中间件test,这样路由组中所有的路由都会使用这个中间件,test中间件在Http/Middleware/TestMiddleware.php中 //同时中间件在Kernel.php中声明.发给这个路由组的参数会转发给中间件,再由中间件决定跳转逻辑 Route::group(['middleware' => 'test'], function () { Route::get('/write/laravelacademy', function () { //使用Test中间件 }); Route::get('/update/laravelacademy', function () { //使用Test中间件 }); }); //当年龄小于18时的路由,这里也使用as来给路由命名叫refuse Route::get('/age/refuse', ['as' => 'refuse', function () { return "未成年人禁止入内!"; }]); //通过domain来给路由组设置子域名,{service}就是域名占位符,比如update.laravel.app Route::group(['domain' => '{service}.laravel.app'], function () { Route::get('/write/laravelacademy', function ($service) { return "Write FROM {$service}.laravel.app"; }); Route::get('/update/laravelacademy', function ($service) { return "Update FROM {$service}.laravel.app"; }); }); //通过prefix指定路由前缀,http://laravel.app:8000/laravelacademy/5.1/write Route::group(['prefix' => 'laravelacademy/{version}'], function () { Route::get('write', function ($version) { return "Write LaravelAcademy {$version}"; }); Route::get('update', function ($version) { return "Update LaravelAcademy {$version}"; }); }); //测试CSRF //生成CSRF,Laravel默认检验CSRF的Token,如果表单没有提交Token则会触发Exception //并不是所有请求都需要验证,所以可以在app/Http/Middleware/VerifyCsrfToken.php中设置要排除的URL Route::get('testCsrf', function () { $csrf_field = csrf_field(); $html = <<<GET <form method="POST" action="testCsrf"> {$csrf_field} <input type="submit" value="Test"/> </form> GET; return $html; }); Route::post('testCsrf', function () { return 'Success!'; }); //中间件参数test1:male就是传递参数给中间件test1 Route::group(['middleware' => 'test1:male'], function () { Route::get('/write/laravelacademy', function () { //使用Test中间件 }); Route::get('/update/laravelacademy', function () { //使用Test中间件 }); }); Route::get('/age/refuse', ['as' => 'refuse', function () { return "18岁以上男子才能访问!"; }]); //声明资源路由 Route::resource('post','PostController');
|