本篇文章為大家展示了怎么在php中利用Laravel定義路由,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站主營嶺東網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),嶺東h5小程序開發(fā)搭建,嶺東網(wǎng)站營銷推廣歡迎嶺東等地區(qū)企業(yè)咨詢
1、執(zhí)行速度快。2、具有很好的開放性和可擴(kuò)展性。3、PHP支持多種主流與非主流的數(shù)據(jù)庫。4、面向?qū)ο缶幊蹋篜HP提供了類和對象。5、版本更新速度快。6、具有豐富的功能。7、可伸縮性。8、功能全面,包括圖形處理、編碼與解碼、壓縮文件處理、xml解析等。
1.get方式
Route::get('/', function () { return view('welcome'); });
好處是把回調(diào)的實(shí)現(xiàn)和使用場所隔離,這段代碼是意思是,當(dāng)get方式訪問根目錄時(shí)返回welcome視圖。
2.info方法
Route::get('admin/info','Admin\IndexController@info');
訪問http://your-app.dev/admin/info時(shí),調(diào)用命名空間Admin下的IndexController控制器里的info方法。這樣寫是不是麻煩,所以有了路由組,統(tǒng)一指定中間件,前綴和命名空間。
3.使用路由
//在瀏覽器直接訪問,跳轉(zhuǎn)到welcome視圖,視圖路徑在public/resources/views/ Route::get('/', function () { return view('welcome'); }); //在瀏覽器直接訪問,返回hello world Route::get("route1", function () { return "hello world"; }); //因?yàn)槭莗ost請求,不可以在瀏覽器地址欄直接訪問 Route::post("route2", function () { return "hello world"; }); //match: 可以定義接收get或post請求 Route::match(['get','post'], "reute3", function () { return "hello world"; }); //any: 可以接收get和post請求 Route::any("route4", function () { return "hello world"; }); //路由接收參數(shù),php中的字符串拼接用 "." Route::get("user1/{id}", function($id){ return "id-->" . $id; }); //在user2/{id?} 后面加個(gè)問號代表參數(shù)不是必要的,可以在function中給個(gè)默認(rèn)值 Route::get("user2/{id?}", function($id = 5){ return "id-->" . $id; }); //參數(shù)校驗(yàn),在最后面加上正折表達(dá),這里代表username只能是字母 Route::get("user3/{username}", function($username) { return "username-->" . $username; })->where("username","[A-Za-z]+"); //路由別名,只要在后面加個(gè)數(shù)組,注意數(shù)組第一個(gè)元素是"as" => "center" ,第二個(gè)元素是function Route::get("user4/mamber-center", ["as" => "center" , function(){ //使用routes可以返回對應(yīng)的路徑 return route("center"); }]); //路由群組,加前綴,member.可以通過member/user1訪問到第一個(gè), //通過member/user2訪問到第二個(gè) Route::group(["prefix" => "member"], function(){ Route::get("user1",function(){ return "hello world"; }); Route::get("user2",["as" => "group_route", function(){ return route("group_route"); }]); });
上述內(nèi)容就是怎么在php中利用Laravel定義路由,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文題目:怎么在php中利用Laravel定義路由
當(dāng)前URL:http://jinyejixie.com/article10/pgshdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、商城網(wǎng)站、面包屑導(dǎo)航、網(wǎng)頁設(shè)計(jì)公司、ChatGPT、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)