douyan2002 2018-09-04 12:03
浏览 146
已采纳

中间件功能不存在

In my web.php I have a route

Route::get('summary_average_fee', 'Summary@AverageFee')->middleware('CheckParams@dateLimits');

Im trying to refrence the dateLimits function in the CheckParams class

My CheckParams class, saved as CheckParams.php in the Middleware folder

<?php

namespace App\Http\Middleware;

use Closure;

class CheckParams
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function dateLimits($request, Closure $next)
    {
        isEmpty($request->input('startDate'), 'NO_START_DATE');
        isEmpty($request->input('endDate'), 'NO_END_DATE');

        return $next($request);
    }

    private function isEmpty($value, $error, $status)
    {
        if(empty($value))
        {
            return response()->json($error, 422);
        }
    }
}

In the kernal.php file I add this to the routeMiddleware array

'CheckParams' => \App\Http\Middleware\CheckParams::class

When it runs, I get the error that Class CheckParams@dateLimits does not exist

  • 写回答

1条回答 默认 最新

  • dongzaotiao2863 2018-09-04 12:07
    关注

    Seems to me that your middleware should be rewritten and update the usage:

    use App\Http\Middleware\CheckParams;
    
    Route::get('summary_average_fee', 'Summary@AverageFee')->middleware(CheckParams::class);
    

    Middleware:

    namespace App\Http\Middleware;
    
    use Closure;
    
    class CheckParams
    {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @return mixed
         */
        public function handle($request, Closure $next)
        {
            $this->isEmpty($request->input('startDate'), 'NO_START_DATE');
            $this->isEmpty($request->input('endDate'), 'NO_END_DATE');
    
            return $next($request);
        }
    
        private function isEmpty($value, $error, $status)
        {
            if(empty($value))
            {
                return response()->json($error, 422);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?