Laravel中路由基礎知識點總結

    在laravel5中,通常在 app/Http/routes.php 中定義應用中的大多數路由,這個文件加載了 App\Providers\RouteServiceProvider 類。 大多數基本的 Laravel 路由都只接受一個 URI 一個閉包(Closure) 參數;

    下面是一些基本路由使用方法與解釋。

<?php

 

//當打開http://localhost:8080時顯示/resources/views/vender/welcome.php網頁的內容;get表示通過get方法;

Route::get('/', function () {

   return view('welcome');

});

 

Blade::setRawTags('{{', '}}');

 

//當打開地址爲http://localhost:8080/music時,顯示function()的內容,  音樂列表

Route::get('music',function(){

   return '音樂列表...';

});

 

//Route::post('movie',function(){

//   return '發佈電影...';

//});

 

正則表達式:

//http://localhost:8080/movie/12345顯示  電影列表12345

Route::get('movie/{movie_id}',function($movie_id){

   return '電影列表'.$movie_id;

})

//->where('movie_id','[0-9]+'); //表示匹配數字,/movie/後只能是任意數字

->where('movie_id','[a-z]+');  //表示匹配字母,/movie/後只能是任意字母

 

在路由中將數據指定到視圖

//傳遞數據到視圖裏,方法有幾種

Route::get('movie',function(){

//  returnview('movie.index')->with('user','Edom'); //movie是文件夾,index表示index.php或者index.blade.php;

 // ②   returnview('movie.index')->withUser('Edom')->withEmail('[email protected]');

   ③  $data = array(

       'user' => 'Edom',

       'email' => '[email protected]'

   );

   $data_block = array(

       'block_title' => '電影排行榜'

   );

   returnview('movie.index',$data)->nest('boxoffice','movie.block.boxoffice',$data_block);//把子視圖嵌入到視圖裏面;nest方法三個參數的意義:①自定義名字②所要導入的php文件存放位置③數據

});

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章