[Laravel] Laravel的基本HTTP路由
使用Laravel的基本路由,實作get請求回應,找到檔案app/Http/routes.php
呼叫Route的靜態方法get(),實作程式回應,參數:string類型回應的路徑,匿名函數function(){}
匿名函數內部,返回string資料
實作post,put,delete的請求,同上
實作get傳遞參數的路由,呼叫Route的靜態方法get(),參數:路徑,匿名函數
路徑,大括號包裹參數名,不含$,例如:'/user/{id}'
匿名函數,接收參數,例如:function($id){}
[Laravel ] Laraval的基本控制器
在app/Http/Controllers目錄下,新建一個Index/IndexController.php
定義命名空間,namespace AppHttpControllersIndex
引入ControllerpControluse,繼承3Htt實作方法index,回傳資料
定義路由指定控制器的行為,例如:Route::get("/index","IndexIndexController@index");,
注意命名空間部分,新建的控制器是在根命名空間下面,指定的時候加入自己新加的命名空間
[Laravel] Laravel的基本視圖
在目錄resources/views/下面,建立index/index.php
在控制器中使用函數view()來呼叫模板,參數:檔案路徑(.分隔目錄),資料
路由:routes.php
<?<span>php </span><span>/*</span><span>|-------------------------------------------------------------------------- | 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. | </span><span>*/</span><span>/*</span><span>测试get post</span><span>*/</span><span> Route::get(</span>'/'<span>, function () { $url</span>=url("index"<span>); </span><span>return</span> "Hello World"<span>.$url; </span><span>//</span><span>return view('welcome');</span><span>}); Route::post(</span>"/post"<span>,function(){ </span><span>return</span> "测试post"<span>; }); </span><span>/*</span><span>传递参数</span><span>*/</span><span>Route::get(</span>"/user/{id}"<span>,function($id){ </span><span>return</span> "用户"<span>.$id; }); </span><span>/*</span><span>使用控制器</span><span>*/</span><span>Route::get(</span>"/index","Index\IndexController@index"<span>); </span><span>/*</span><span>|-------------------------------------------------------------------------- | 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. | </span><span>*/</span><span> Route::group([</span>'middleware' => ['web'<span>]], function () { </span><span>// </span>});
控制器:IndexController.php
<?<span>php namespace App\Http\Controllers\Index; use App\Http\Controllers\Controller; </span><span>class</span> IndexController <span>extends</span><span> Controller{ </span><span>public</span><span> function index(){ $data</span>=<span>array(); $data[</span>'title']="Index控制器"<span>; </span><span>return</span> view("index.index"<span>,$data); } }</span>
模板:index.php
以上就介紹了[Laravel] Laravel的基本使用,包括了laravel方面的內容,希望對PHP教程有興趣的朋友有所幫助。