「路徑」方法用於檢索請求的URI。「is」方法用於檢索在該方法的引數指定請求URI的模式匹配。要獲得完整的URL,我們可以使用「url」的方法。
php artisan make:controller UriController
app/Http/Controllers/UriController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class UriController extends Controller { public function index(Request $request){ // Usage of path method $path = $request->path(); echo 'Path Method: '.$path; echo '<br>'; // Usage of is method $pattern = $request->is('foo/*'); echo 'is Method: '.$pattern; echo '<br>'; // Usage of url method $url = $request->url(); echo 'URL method: '.$url; } }
app/Http/route.php
Route::get('/foo/bar','UriController@index');
http://localhost:8000/foo/bar
Laravel 很容易地檢索輸入值。 不管使用什麼方法:「get」或「post」,Laravel方法對於這兩種方法檢索的輸入值的方法是相同的。有兩種方法我們可以用來檢索輸入值。
input() 方法接受一個引數,在表單中的欄位的名稱。例如,如果表單中包含 username 欄位那麼可以通過以下方式進行存取。
$name = $request->input('username');
$request->username
第1步 - 建立一個表單:Registration ,在這裡使用者可以註冊自己並儲存表單:resources/views/register.php
<html> <head> <title>Form Example</title> </head> <body> <form action = "/user/register" method = "post"> <input type = "hidden" name = "_token" value = "<?php echo csrf_token() ?>"> <table> <tr> <td>名字:</td> <td><input type = "text" name = "name" /></td> </tr> <tr> <td>使用者名:</td> <td><input type = "text" name = "username" /></td> </tr> <tr> <td>密碼:</td> <td><input type = "text" name = "password" /></td> </tr> <tr> <td colspan = "2" align = "center"> <input type = "submit" value = "Register" /> </td> </tr> </table> </form> </body> </html>
php artisan make:controller UserRegistration
app/Http/Controllers/UserRegistration.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class UserRegistration extends Controller { public function postRegister(Request $request){ //Retrieve the name input field $name = $request->input('name'); echo 'Name: '.$name; echo '<br>'; //Retrieve the username input field $username = $request->username; echo 'Username: '.$username; echo '<br>'; //Retrieve the password input field $password = $request->password; echo 'Password: '.$password; } }
app/Http/routes.php
Route::get('/register',function(){ return view('register'); }); Route::post('/user/register',array('uses'=>'UserRegistration@postRegister'));
第6步 - 請存取以下網址,登錄檔單如下圖所示。輸入註冊資訊,然後點選"註冊",之後會看到檢索並顯示使用者註冊的詳細資訊在第二個頁面上。
http://localhost:8000/register