【问题标题】:laravel $request is undefined in production, locally it workslaravel $request 在生产中未定义,在本地工作
【发布时间】:2018-09-04 21:29:18
【问题描述】:

我在使用 Laravel 和 Request 时遇到了一些奇怪的问题:

在我的控制器中,我有索引:

use Illuminate\Http\Request;
/**
 * Display a listing of the resource.
 *
 * @param Request $request
 * @param $id
 * @return \Illuminate\Http\Response
 */
public function index(Request $request, $id) {
   try {
        $from = Carbon::parse($request->query->get('from', '1970-01-01'));
        $till = Carbon::parse($request->query->get('till', Carbon::now()))->endOfDay();
    } catch (\Exception $e) {
        return response()->json([
            'error' => 'Invalid time range or data',
        ], 400);
    } 
}

在我的本地机器上工作。 但是当我将它部署到生产环境并在 catch (\Exception $e) 中转储($e) 时,我得到以下异常:

$message: "未定义变量:请求"

所以我改成

use App\Api1\Requests\TicketRequest;

public function index(TicketRequest $request, $id)

在本地和生产服务器上工作。

TicketRequest.php 只是扩展了请求:

class TicketRequest extends Request

谁能告诉我,为什么 $request 在生产环境中未定义,但在本地没有定义?

提前致谢。

【问题讨论】:

  • 你需要 import requestin your class use Illuminate\Http\Request;
  • 不管是什么类,$request 永远不会在这里未定义,你没有看对地方。
  • $message: "Undefined variable: request" -- $message 在哪里..?
  • @BagusTesa - 我必须在生产中转储($e)才能收到此消息。
  • 仍然不起作用, $request 在此代码块/范围内实际上不能未定义。它是一个函数参数。显示错误的回溯

标签: php laravel request


【解决方案1】:

试试这个

use Illuminate\Http\Request;
/**
 * Display a listing of the resource.
 *
 * @param Request $request
 * @param $id
 * @return \Illuminate\Http\Response
 */
public function index(Request $request, $id) {
try {
    $from = \Carbon::parse($request->query->get('from', '1970-01-01'));
    $till = \Carbon::parse($request->query->get('till', \Carbon::now()))->endOfDay();
} catch (\Exception $e) {
    return response()->json([
        'error' => 'Invalid time range or data',
    ], 400);
} 

}

【讨论】:

  • 我在另一个控制器中有几乎相同的代码并且它可以工作: public function index(ProductSaleRequest $request, $resellerId) { try { $from = Carbon::parse($request->query->得到('来自','1970-01-01')); $till = Carbon::parse($request->query->get('till', Carbon::now()))->endOfDay(); } catch (\Exception $e) { return response()->json([ 'error' => '无效的时间范围或数据', ], 400); } 所以我不认为 Carbon 与它有任何关系。
  • 你尝试运行composer updatecomposer dump-autoload??
  • 是的。正如我在上面评论的那样,它更多的问题是为什么它不能在生产服务器上工作,但在本地却可以...... composer.lock 在两者上都是相同的。服务器也是 dockerized 所以应该没有任何问题..
猜你喜欢
  • 2020-04-03
  • 2021-08-03
  • 2021-06-08
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 2017-07-15
  • 2017-08-06
  • 2021-06-14
相关资源
最近更新 更多