tesoner tesoner - 13 days ago 5
PHP Question

Error with an api route - Laravel 5.3

I'm trying to access to this route: http://anaketesting.tk/product-service/payment-notification Really is a route for api, but consuming the route, have same error that browser.

My route try 1:

Route::get('/product-service/payment-notification', "ProductServiceController@notification")->name('productService.notification');


My route try 2:

Route::get('/product-service/payment-notification', function(){
return \Response::json([
'CREATED' => true
], 201); #also i tryed return 201 directly...
});


My route try 3:

Route::get('product-service/payment-notification', [
'as' => 'productService.notification',
'uses' => 'ProductServiceController@notification'
]);


My notification m├ęthod

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);
}


I have not an storage/logs error with this method, is as was ignored. Please help ;)

Answer

see the RouteServiceProvider of Laravel 5.3, it shows that api routes being grouped and prefixed with api by default.

/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');
    });
}

So you need to prefix api in your url. for example

to call this route

Route::get('/product-service/payment-notification', "ProductServiceController@notification")->name('productService.notification'); 

you need to call

http://anaketesting.tk/api/product-service/payment-notification

not

http://anaketesting.tk/product-service/payment-notification