“路径”方法用于检索请求的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