-3

我有这个问题。当我创建新的中间件并编写此代码时

这是一个错误文本语法错误,意外的 'printf' (T_STRING),期望函数 (T_FUNCTION) 或 const (T_CONST)

namespace App\Http\Middleware;
use Carbon\Carbon;
use Closure;
use Illuminate\Http\Request;

class CheckTime
{
    printf("Right now is %s", Carbon::now()->toDateTimeString());

    // $now = Carbon::now();
    // $start = Carbon::createFromTimeString('00:00');
    // $end = Carbon::createFromTimeString('06:00');


    if ($now->between($start, $end)) {
        return redirect('/')->with('limitTime, You cant write post at this time');
    }

    return $next($request);
    public function handle(Request $request, Closure $next)
}
4

1 回答 1

2

您不能在类中的函数之外有这样的语句

namespace App\Http\Middleware;
use Carbon\Carbon;
use Closure;
use Illuminate\Http\Request;

class CheckTime
{
    
    public function handle(Request $request, Closure $next)
    {
        printf("Right now is %s", Carbon::now()->toDateTimeString());

        $now = Carbon::now();
        $start = Carbon::createFromTimeString('00:00');
        $end = Carbon::createFromTimeString('06:00');


        if ($now->between($start, $end)) {
            return redirect('/')->with('limitTime, You cant write post at this time');
        }

        return $next($request);
    }
}
于 2020-12-10T06:30:56.213 回答