[Laravel] Laravel的基本HTTP路由 使用Laravel的基本路由,實現get請求響應,找到文件app/Http/routes.php 調用Route的靜態方法get(),實現get響應,參數:string類型的路徑,匿名函數function(){} 匿名函數內部,返回string數 ...
[Laravel] Laravel的基本HTTP路由
使用Laravel的基本路由,實現get請求響應,找到文件app/Http/routes.php
調用Route的靜態方法get(),實現get響應,參數:string類型的路徑,匿名函數function(){}
匿名函數內部,返回string數據
實現post,put,delete的請求,同上
實現get傳遞參數的路由,調用Route的靜態方法get(),參數:路徑,匿名函數
路徑,大括弧包裹參數名,不含$,例如:’/user/{id}’
匿名函數,接收參數,例如:function($id){}
[Laravel] Laraval的基本控制器
在app/Http/Controllers目錄下,新建一個Index/IndexController.php
定義命名空間,namespace App\Http\Controllers\Index
引入Controller基本控制器,use App\Http\Controllers\Controller
定義IndexController繼承Controller
實現方法index,返回數據
定義路由指定控制器的行為,例如:Route::get("/index","Index\IndexController@index");,
註意命名空間部分,新建的控制器是在根命名空間下麵,指定的時候添加自己新加的命名空間
[Laravel] Laravel的基本視圖
在目錄resources/views/下麵,創建index/index.php
在控制器中使用函數view()來調用模板,參數:文件路徑(.分隔目錄),數據
路由:routes.php
<?php /* |-------------------------------------------------------------------------- | Routes File |-------------------------------------------------------------------------- | | Here is where you will register all of the routes in an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ /*測試get post*/ Route::get('/', function () { $url=url("index"); return "Hello World".$url; //return view('welcome'); }); Route::post("/post",function(){ return "測試post"; }); /*傳遞參數*/ Route::get("/user/{id}",function($id){ return "用戶".$id; }); /*使用控制器*/ Route::get("/index","Index\IndexController@index"); /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | This route group applies the "web" middleware group to every route | it contains. The "web" middleware group is defined in your HTTP | kernel and includes session state, CSRF protection, and more. | */ Route::group(['middleware' => ['web']], function () { // });
控制器:IndexController.php
<?php namespace App\Http\Controllers\Index; use App\Http\Controllers\Controller; class IndexController extends Controller{ public function index(){ $data=array(); $data['title']="Index控制器"; return view("index.index",$data); } }
模板:index.php
<body> <div class="container"> <div class="content"> <div class="title"><?php echo $title;?></div> </div> </div> </body>