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




