[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
phpnamespace 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>
以上就介紹了[Laravel] Laravel的基本使用,包括了laravel方面的內容,希望對PHP教程有興趣的朋友有所協助。