api 路线错误 - Laravel 5.3
Error with an api route - Laravel 5.3
我正在尝试访问此路由:http://anaketesting.tk/product-service/payment-notification确实是 api 的路由,但使用该路由时,出现与浏览器相同的错误。
我的路线尝试 1:
Route::get('/product-service/payment-notification', "ProductServiceController@notification")->name('productService.notification');
我的路线尝试2:
Route::get('/product-service/payment-notification', function(){
return \Response::json([
'CREATED' => true
], 201); #also i tryed return 201 directly...
});
我的路线尝试3:
Route::get('product-service/payment-notification', [
'as' => 'productService.notification',
'uses' => 'ProductServiceController@notification'
]);
我的通知方式
public function notification(Request $request){
$date = Carbon::now();
$date = $date->format('Ymdhis');
file_put_contents(storage_path().'/notification_'.$date.'.json', \Response::json($request));
return \Response::json([
'CREATED' => true
], 201);
}
我没有使用此方法出现 storage/logs 错误,因为它被忽略了。请帮助 ;)
查看Laravel 5.3的RouteServiceProvider,显示api路由默认分组前缀为api .
/app/Providers/RouteServiceProvider.php
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
因此您需要在 url 中加上前缀 api。例如
调用此路由
Route::get('/product-service/payment-notification', "ProductServiceController@notification")->name('productService.notification');
你需要打电话给
http://anaketesting.tk/api/product-service/payment-notification
不是
在 TRY3 中,由于您的系统正在跳过一些日志,因此可能存在两个不同的问题
- 您的系统没有足够的权限将数据写入日志文件。为了使用它,您必须授予对您的日志文件的权限。
- 您可以使用 CustomException 处理这些类型的异常,
转到App/Exceptions/Handler。php
public function render($request, Exception $exception)
{
if($exception instanceof RouteNotFoundException){
abort(404,'Invalid Route Requested');
}else{
return parent::render($request, $exception);
}
}
我正在尝试访问此路由:http://anaketesting.tk/product-service/payment-notification确实是 api 的路由,但使用该路由时,出现与浏览器相同的错误。
我的路线尝试 1:
Route::get('/product-service/payment-notification', "ProductServiceController@notification")->name('productService.notification');
我的路线尝试2:
Route::get('/product-service/payment-notification', function(){
return \Response::json([
'CREATED' => true
], 201); #also i tryed return 201 directly...
});
我的路线尝试3:
Route::get('product-service/payment-notification', [
'as' => 'productService.notification',
'uses' => 'ProductServiceController@notification'
]);
我的通知方式
public function notification(Request $request){
$date = Carbon::now();
$date = $date->format('Ymdhis');
file_put_contents(storage_path().'/notification_'.$date.'.json', \Response::json($request));
return \Response::json([
'CREATED' => true
], 201);
}
我没有使用此方法出现 storage/logs 错误,因为它被忽略了。请帮助 ;)
查看Laravel 5.3的RouteServiceProvider,显示api路由默认分组前缀为api .
/app/Providers/RouteServiceProvider.php
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
Route::group([
'middleware' => 'api',
'namespace' => $this->namespace,
'prefix' => 'api',
], function ($router) {
require base_path('routes/api.php');
});
}
因此您需要在 url 中加上前缀 api。例如
调用此路由
Route::get('/product-service/payment-notification', "ProductServiceController@notification")->name('productService.notification');
你需要打电话给
http://anaketesting.tk/api/product-service/payment-notification
不是
在 TRY3 中,由于您的系统正在跳过一些日志,因此可能存在两个不同的问题
- 您的系统没有足够的权限将数据写入日志文件。为了使用它,您必须授予对您的日志文件的权限。
- 您可以使用 CustomException 处理这些类型的异常,
转到App/Exceptions/Handler。php
public function render($request, Exception $exception)
{
if($exception instanceof RouteNotFoundException){
abort(404,'Invalid Route Requested');
}else{
return parent::render($request, $exception);
}
}