laravel入门之路由
jianfly.com 2019-01-31 1546次浏览
<?php //路由 //1、GET Route::get('basic1', function() { return 'Hello World'; //访问public/basic1 }) //2、POST Route::post('basic2', function() { return 'Hello World'; }) //3、多请求路由 Route::match(['get', 'post'], 'multy1', function() { return 'Hello World'; // 访问public/multy1 }) //4、any Route::any('multy2', function() { return 'Hello World'; // 访问public/multy2 }) //路由参数 Route::any('user/{id}', function( $id ) { return 'User-' . $id; //访问public/user/4,返回User-4 }) Route::any('user/{name?}', function( $name = 'sean' ) { return 'User-' . $name; //访问public/user/kkkk,返回User-kkkk }) //正则 Route::any('user/{name?}', function( $name = 'sean' ) { return 'User-' . $name; //访问public/user/kkkk,返回User-kkkk })->where('name', '[A-Aa-z]'); //多个参数 Route::any('user/{id}/{name?}', function( $id, $name = 'sean' ) { return 'User-'.$id . $name; })->where(['id' => '[0-9]' , 'name' => '[A-Aa-z]']); //路由别名 Route::get('user/member-center', ['as' => 'center', function() { return 'member-center'; //访问public/user/member-center return route('center'); //生成对应的url,改member-center时,其他地方不需要修改 }]); //路由群组 Route::group(['prefix'=>'member'], function(){ Route::get('user/center', ['as' => 'center', function() { return route('center'); //public/member/user/center }]); Route::any('multy2', function() { return 'Hello World'; // 访问public/member/multy2 }) }) //路由中输出识图 Route::get('view', function() { return view('welcome'); }) //控制器 //MemberController.php namespace APP\Http\Controllers; class MemberController extends Controller { public function info() { return 'member-info'; } } Route::get('member/info', 'MemberController@info'); Route::get('member/info', ['uses' => 'MemberController@info']); Route::get('member/info', ['uses' => 'MemberController@info', 'as'=>'memberinfo']); //参数绑定 Route::any('member/info/{id}', 'MemberController@info')->where('id','[0-9]+'); public function info( $id ) { return $id; } //视图 public function info( $id ) { return view('member-info', ['name'=>111, 'age'=>222]); // {{$name}} } //模型 namespace App; use Illuminate\Database\Eloquent\Model; class Member extends model { public static function getMember() { reutrn 'asdsadadaw'; } } //调用 return Member::getMember();