目录

Laravel - 请求( Request)

在本章中,您将详细了解Laravel中的请求。

检索请求URI

path方法用于检索请求的URI。 is方法用于检索所请求的URI,该URI与方法参数中指定的特定模式匹配。 要获取完整的URL,我们可以使用url方法。

例子 (Example)

Step 1 - 执行以下命令以创建名为UriController的新控制器。

php artisan make:controller UriController –plain

Step 2 - 成功执行URL后,您将收到以下输出 -

UriController

Step 3 - 创建控制器后,在该文件中添加以下代码。

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;
   }
}

Step 4 - 在app/Http/route.php文件中添加以下行。

app/Http/route.php

Route::get('/foo/bar','UriController@index');

Step 5 - 访问以下URL。

http://localhost:8000/foo/bar

Step 6 - 输出将如下图所示。

路径方法

检索输入

可以在Laravel中轻松检索输入值。 无论使用什么方法getpost ,Laravel方法都将以相同的方式检索两个方法的输入值。 我们可以通过两种方式检索输入值。

  • 使用input()方法
  • 使用Request实例的属性

Using the input() method

input()方法接受一个参数,即表单中字段的名称。 例如,如果表单包含用户名字段,那么我们可以通过以下方式访问它。

$name = $request->input('username');

使用Request实例的属性

input()方法一样,我们可以直接从请求实例获取username属性。

$request->username

例子 (Example)

请观察以下示例以了解有关请求的更多信息 -

Step 1 - 创建注册表单,用户可以在其中注册自己并将表单存储在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>Name</td>
               <td><input type = "text" name = "name" /></td>
            </tr>
            <tr>
               <td>Username</td>
               <td><input type = "text" name = "username" /></td>
            </tr>
            <tr>
               <td>Password</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>

Step 2 - 执行以下命令以创建UserRegistration控制器。

php artisan make:controller UserRegistration --plain

Step 3 - 成功执行上述步骤后,您将收到以下输出 -

用户注册

Step 4 - 复制以下代码

app/Http/Controllers/UserRegistration.php控制器。

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;
   }
}

Step 5 - 在app/Http/routes.php文件中添加以下行。

app/Http/routes.php

Route::get('/register',function(){
   return view('register');
});
Route::post('/user/register',array('uses'=>'UserRegistration@postRegister'));

Step 6 - 访问以下URL,您将看到注册表单,如下图所示。 输入注册详细信息并单击注册,您将在第二页上看到我们已检索并显示用户注册详细信息。

http://localhost:8000/register

Step 7 - 输出看起来如下图所示。

注册
↑回到顶部↑
WIKI教程 @2018