1. 程式人生 > 程式設計 >Laravel框架路由與MVC例項詳解

Laravel框架路由與MVC例項詳解

本文例項講述了Laravel框架路由與MVC。分享給大家供大家參考,具體如下:

1、路由

路由的作用就是將使用者的不同url請求轉發給相應的程式進行處理,laravel的路由定義在routes資料夾中,預設提供了四個路由檔案,其中web.php檔案定義基本頁面請求。

1.1、基本路由

最基本的路由請求是get與post請求,laravel通過Route物件來定義不同的請求方式。例如定義一個url為'req'的get請求,返回字串‘get response':

Route::get('req',function (){
  return 'get response';
});

當我以get的方式請求http://localhost/Laravel/laravel52/public/req

時,返回如下:

同理,當定義post請求時,使用Route::post(url,function(){});

1.2、多請求路由

如果希望對多種請求方式採用相同的處理,可以使用match或any:

使用match來匹配對應的請求方式,例如當以get或post請求req2時,都返回match response:

Route::match(['get','post'],'req2',function (){
  return 'match response';
});

any會匹配任意請求方式,例如以任意方式請求req3,返回any response:

Route::any('req3',function (){
  return 'any response';
});

1.3、請求引數

必選引數:當以帶引數的形式傳送請求時,可以在路由中進行接收,用大括號將引數括起,用/分割,例如:

Route::get('req4/{name}/{age}',function ($name,$age) {
  return "I'm {$name},{$age} years old.";
});

以get請求時將引數傳遞,結果如下:

可選引數:以上的引數是必須的,如果缺少某一個引數就會報錯,如果希望某個引數是可選的,可以為它加一個?,並設定預設值,預設引數必須為最後一個引數,否則放中間沒法識別:

Route::get('req4/{name}/{age?}',$age=0) {
  return "I'm {$name},{$age} years old.";
});

正則校驗:可以通過where對請求中的引數進行校驗

Route::get('req4/{name}/{age?}',{$age} years old.";
})->where(['name'=>'[A-Za-z]+','age'=>'[0-9]+']);

1.4、路由群組

有時我們的路由可能有多個層級,例如定義一級路由home,其下有二級路由article,comment等,這就需要將article與comment放到home這個群組中。通過陣列鍵prefix為路由article新增字首home:

Route::group(['prefix' => 'home'],function () {
  Route::get('article',function () {
    return 'home/article';
  });
});

這樣通過home/article就可以訪問到該路由了。

1.5、路由命名

有時需要給路由起個名字,需要在定義路由時使用as陣列鍵來指定路由名稱。例如將路由home/comment命名為comment,在生成url與重定向時就可以使用路由的名字comment:

Route::get('home/comment',['as'=>'comment',function(){
  return route('comment');    //通過route函式生成comment對應的url
}]);

輸出為http://localhost/Laravel/laravel52/public/home/comment

2、控制器

route路由只對請求進行分配跳轉,具體的業務邏輯則需要由控制器來處理,控制器一般封裝成為一個php類。控制器的檔案一般放在app/Http/Controlers資料夾下。例如新建一個LoginController類繼承自Controller,定義checkLog方法迴應登入請求,

namespace App\Http\Controllers;
class LoginController extends Controller
{
  public function checkLog($name){
    return $name.'登入成功';
  }
}

在route.php中將login請求分配到checkLog方法:

Route::get('login/{name}','LoginController@checkLog');

同樣的,可以為控制器路由起個名字,比如將其命名為login:

Route::get('login/{name}',['uses'=>'LoginController@checkLog','as'=>'login']);

3、檢視

controller負責處理應用的邏輯,應用的顯示則由檢視View負責,這體現了MVC中不同的邏輯之間的分離。檢視一般位於/resource/views目錄下,一般一個controller檔案對應一個檢視資料夾,因此我建立的檢視為:views/Login/login.blade.php。blade檔案是laravel的一個模板引擎,它編譯為PHP儲存起來。它包含HTML語言,可以在其中直接使用PHP,例如login.blade.php:

<!DOCTYPE Html>
<html>
  <head>
    <title>登入介面</title>
  </head>
  <body>
    {{$name}}登入成功
  </body>
</html>

在controller的checkLog方法中呼叫檢視並傳入引數:

public function checkLog($name){
  return View('Login/login',[
    "name"=>$name
  ]);
}

4、模板

在mvc中通過Models與資料庫中的表進行互動,每個資料庫對應一個Model模板。laravel並沒有定義models目錄,一般可以在app目錄下新建一個models資料夾存放模板檔案。例如定義一個Student模板並指定表名與主鍵:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
  protected $table='student';
  protected $primaryKey='id';
}

在controller中通過Student模板查詢所有:

namespace App\Http\Controllers;
use App\Models\Student;
class Login
{
  public static function showDB(){
    $table=Student::all();
    dd($table);
  }
}

更多關於Laravel相關內容感興趣的讀者可檢視本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《php面向物件程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧彙總》

希望本文所述對大家基於Laravel框架的PHP程式設計有所幫助。