【问题标题】:php fatal error: cannot redeclare App\Exceptions\Handler::render() in G:\Xampp\htdocs\blog\app\Exceptions\Handler.php on line 55php 致命错误:无法在第 55 行的 G:\Xampp\htdocs\blog\app\Exceptions\Handler.php 中重新声明 App\Exceptions\Handler::render()
【发布时间】:2020-06-29 10:00:42
【问题描述】:

我正在使用 laravel 5.8 创建用户角色和权限,并且当我想运行 PermissionTableSeeder 并在 cmd 中运行时,如下所示:

php artisan db:seed --class=PermissionTableSeeder .

但它显示此错误:

php 致命错误:无法重新声明 App\Exceptions\Handler::render() in G:\Xampp\htdocs\blog\app\Exceptions\Handler.php 在第 55 行,这是 我的代码:

<?php

namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;

class Handler extends ExceptionHandler {
/**
 * A list of the exception types that are not reported.
 *
 * @var array
 */
protected $dontReport = [
    //
];

/**
 * A list of the inputs that are never flashed for validation exceptions.
 *
 * @var array
 */
protected $dontFlash = [
    'password',
    'password_confirmation',
];

/**
 * Report or log an exception.
 *
 * @param  \Throwable  $exception
 * @return void
 *
 * @throws \Exception
 */
public function report(Throwable $exception)
{
    parent::report($exception);
}

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Throwable  $exception
 * @return \Symfony\Component\HttpFoundation\Response
 *
 * @throws \Throwable
 */
public function render($request, Throwable $exception)
{
    return parent::render($request, $exception);
}
public function render($request, Exception $exception) {
if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
    return response()->json(['User have not permission for this page access.']);
}

return parent::render($request, $exception);
}

}

【问题讨论】:

  • 您在同一个类中声明了两个名为render() 的函数。同一范围内不能有两个同名的函数。

标签: php laravel cmd laravel-5.8


【解决方案1】:

你不能declare/create两个函数同名如下:

public function render($request, Throwable $exception) {
    return parent::render($request, $exception);
}

public function render($request, Exception $exception) {
   if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
    return response()->json(['User have not permission for this page access.']);
}

为了解决这个删除第一个函数并在下面添加parent::render($request, $exception);

 public function render($request, Exception $exception) {   
    return parent::render($request, $exception); // add here
       if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
        return response()->json(['User have not permission for this page access.']);
    }

【讨论】:

    猜你喜欢
    • 2020-10-03
    • 2020-12-20
    • 2020-10-10
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多