修改laravel中间件中的输入

时间:2015-11-28 19:50:25

标签: php laravel laravel-5 laravel-5.1 laravel-middleware

某些服务向我的网站发出HTTP请求并传递一些输入。这个输入对我来说有点错误的结构,所以我试图修改它。

我制作了一个中间件并将这个中间件附加到我的路线上。 handle方法如下所示:

public function handle($request, Closure $next)
{
    $input = $request->all();

    // Input modification

    $request->replace($input);
    \Log::info($request->all()); // Shows modified request

    return $next($request);
}

然而,在我的控制器中,我得到了旧的输入。我也有点困惑,因为我也使用FormRequest,因为我意识到这两个请求是不同的实体。那我怎样才能修改中间件中的输入?

1 个答案:

答案 0 :(得分:2)

我不知道你的确切问题是什么,但我会告诉你我做了什么让它发挥作用,它可以解决你的问题:

应用/ HTTP /中间件/ TestMiddleware.php

<LinearLayout>
   <MvxListView
        android:id="@+id/ls_contact"
        android:MvxItemTemplate="@layout/Item_Contact_List"
        local:MvxBind="ItemsSource PPL" />

  <Button
        android:id="@+id/btnSubmit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Submit"/> 
</LinearLayout>



Item_Contact_List.axml:


<LinearLayout>
    <TextView
    android:id="@+id/tv_name"
        local:MvxBind="Text Name" />
    <EditText
       android:id="@+id/tv_contactno"
       local:MvxBind="Text ContactNo" />
</LinearLayou



----------- In ViewModel


1. Use the webservice to get the data from BackEnd 

in ViewModel :

 OnCreate()
 { 
   _ppl = WebSvc.getContact() //-- this is webservice

 }


 private People _ppl = new People();


 public People PPL
 {
     get { return this._ppl; }
     set { 
           this._ppl = value; 

          this.RaisePropertyChanged(() => this.PPL); 
        }
 }



2. When user click the Submit button 

Question :

How to iterate the ListView and to get the tv_name and tv_contactno

应用/ HTTP / Kernel.php

<?php namespace App\Http\Middleware;

use Closure;

class TestMiddleware
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $input = $request->all();

        if (isset($input['mod'])) {
            list($input['int'], $input['text']) = explode('-', $input['mod']);
            unset($input['mod']);
            // Input modification
            $request->replace($input);

            \Log::info($request->all()); // Shows modified request
        }

        return $next($request);
    }

}

应用/ HTTP / routes.php文件

protected $middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'App\Http\Middleware\VerifyCsrfToken',
    Middleware\TestMiddleware::class, // this line added
];

应用/ HTTP /请求/ SampleRequest.php

 Route::get('/test', ['uses' => 'TestController@index']);

应用/ HTTP /控制器/ TestController.php

<?php namespace App\Http\Requests;

class SampleRequest extends Request
{        
    public function rules()
    {
        return [
            'int'              =>
                [
                    'required',
                    'integer'
                ],
            'text' => [
                'max: 5',
            ]
        ];
    }
}

在控制台中我运行<?php namespace App\Http\Controllers; use App\Http\Requests; class TestController extends \Illuminate\Routing\Controller { public function index(Requests\SampleRequest $request) { dd($request->all()); } }

现在我运行以下网址:

composer dump-autoload

我从http://testproject.app/test?mod=23-tav 进入控制器:

dd

正如预期的那样,当我以array:2 [▼ "text" => "tav" "int" => "23" ] 为例运行时,由于数据未通过http://testproject.app/test?mod=abc-tav验证(SampleRequest,因此我被重定向到主页。不是整数)