Laravel $ request-> has('name')不起作用 [英] Laravel $request->has('name') doesn't work

查看:484
本文介绍了Laravel $ request-> has('name')不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用laravel学习如何使用它.当前,我正在处理表单,并查看Laravel提供的不同方法来简化我们的工作,但是我看不到以下代码为什么不起作用:

I recently started using laravel to learn how to use it. Currently, I am handling forms and looking at the different methods that Laravel offers to facilitate our work but I can not see why the following code does not work:

public function mensajes(Request $request) {
    //return $request->all();

    if ($request->has('nombre')){
        return "Sí tiene nombre. Es ". $request->input('nombre') ." y su correo es ". $request->input('email');
    } else {
        return "No tiene nombre";
    }



}

我尝试验证表单是否接收到输入的名称",但无论是否接收到,它都将其视为已存在且已被填充,但是,如果尝试正确执行此代码,它将被视为正确.

I try to verify that the form receives the input "name", but it receives it or not, it takes it as if it existed and if it was filled, however, when trying to execute this code if it does it correctly.

public function mensajes(Request $request) {
    //return $request->all();

    if ($request->input('nombre') != ''){
        return "Sí tiene nombre. Es ". $request->input('nombre') ." y su correo es ". $request->input('email');
    } else {
        return "No tiene nombre";
    }



}

我看代码的时间很多,也许太多了,却看不到错误.

I have a lot, maybe too much time watching my code, without seeing the error.

推荐答案

如果不使用SanitizeMiddleware,laravel会将输入名称作为请求的属性保留为空字符串(""),因此->has("name")方法将返回总是true.

If you are not using SanitizeMiddleware laravel will keep the input name as property in the request as an empty string ("") so ->has("name") method will return true always.

在控制台上执行以下操作:

Do the following, on console:

php artisan make:middleware SanitizeMiddleware

然后填充新的中间件

   <?php

namespace App\Http\Middleware;

use Closure;

class SanitizeMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        foreach ($request->input() as $key => $value) {
            if (empty($value) || $value == "") {
                if($value !== '0')
                $request->request->remove($key);
            }
        }

        return $next($request);
    }
}

并在Web中间件组内或您想使用它的任何地方的Kernel上注册它:

And register it on Kernel, within the web middleware group or wherever yo want to use it:

    /**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        SanitizeMiddleware::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

这将从传入请求中删除空输入,因此->has("")现在应该可以使用.

This will remove empty inputs from incoming request so ->has("") should work now.

这篇关于Laravel $ request-> has('name')不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆