[Laravel] Laravel的基本使用
[laravel] laravel的基本http路由
?
使用Laravel的基本路由,實現(xiàn)get請求響應(yīng),找到文件app/Http/routes.php
調(diào)用Route的靜態(tài)方法get(),實現(xiàn)get響應(yīng),參數(shù):string類型的路徑,匿名函數(shù)function(){}
匿名函數(shù)內(nèi)部,返回string數(shù)據(jù)
?
實現(xiàn)post,put,delete的請求,同上
?
實現(xiàn)get傳遞參數(shù)的路由,調(diào)用Route的靜態(tài)方法get(),參數(shù):路徑,匿名函數(shù)
路徑,大括號包裹參數(shù)名,不含$,例如:’/user/{id}’
匿名函數(shù),接收參數(shù),例如:function($id){}
?
[Laravel] Laraval的基本控制器
?
在app/Http/Controllers目錄下,新建一個Index/IndexController.php
定義命名空間,namespace App\Http\Controllers\Index
引入Controller基本控制器,use App\Http\Controllers\Controller
定義IndexController繼承Controller
實現(xiàn)方法index,返回數(shù)據(jù)
定義路由指定控制器的行為,例如:Route::get("/index","Index\[email?protected]");,
注意命名空間部分,新建的控制器是在根命名空間下面,指定的時候添加自己新加的命名空間
?
[Laravel] Laravel的基本視圖
在目錄resources/views/下面,創(chuàng)建index/index.php
在控制器中使用函數(shù)view()來調(diào)用模板,參數(shù):文件路徑(.分隔目錄),數(shù)據(jù)
?
路由:routes.php
?
<?<span style="color: #000000;">php</span><span style="color: #008000;">/*</span><span style="color: #008000;">|--------------------------------------------------------------------------| 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 style="color: #008000;">*/</span><span style="color: #008000;">/*</span><span style="color: #008000;">測試get post</span><span style="color: #008000;">*/</span><span style="color: #000000;"> Route::get(</span>'/'<span style="color: #000000;">, function () { $url</span>=url("index"<span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> "Hello World"<span style="color: #000000;">.$url; </span><span style="color: #008000;">//</span><span style="color: #008000;">return view('welcome');</span><span style="color: #000000;">});Route::post(</span>"/post"<span style="color: #000000;">,function(){ </span><span style="color: #0000ff;">return</span> "測試post"<span style="color: #000000;">;});</span><span style="color: #008000;">/*</span><span style="color: #008000;">傳遞參數(shù)</span><span style="color: #008000;">*/</span><span style="color: #000000;">Route::get(</span>"/user/{id}"<span style="color: #000000;">,function($id){ </span><span style="color: #0000ff;">return</span> "用戶"<span style="color: #000000;">.$id;});</span><span style="color: #008000;">/*</span><span style="color: #008000;">使用控制器</span><span style="color: #008000;">*/</span><span style="color: #000000;">Route::get(</span>"/index","Index\[email protected]"<span style="color: #000000;">);</span><span style="color: #008000;">/*</span><span style="color: #008000;">|--------------------------------------------------------------------------| 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 style="color: #008000;">*/</span><span style="color: #000000;">Route::group([</span>'middleware' => ['web'<span style="color: #000000;">]], function () { </span><span style="color: #008000;">//</span>});
?
控制器:IndexController.php
?
<?<span style="color: #000000;">phpnamespace App\Http\Controllers\Index;use App\Http\Controllers\Controller;</span><span style="color: #0000ff;">class</span> IndexController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Controller{ </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> function index(){ $data</span>=<span style="color: #000000;">array(); $data[</span>'title']="Index控制器"<span style="color: #000000;">; </span><span style="color: #0000ff;">return</span> view("index.index"<span style="color: #000000;">,$data); }}</span>
?
模板:index.php
<span style="color: #0000ff;"><</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="container"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="content"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="title"</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><?</span><span style="color: #ff00ff;">php echo $title;</span><span style="color: #0000ff;">?></span><span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"></</span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
?
每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號