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