let me give you a brief of what i am trying to achieve here ->
i am making an api for a 3 step form.... what i am doing is in each step i am saving the data in session.
and plan to create the record in the final step.
function orderInfo(CreateOrderInfoRequest $request)
{
Session::put('order_info', $request->validated());
return $this->apiResponse(true,'Data Saved Successfully!',200,[]);
}
function departure(CreateDepartureRequest $request)
{
Session::put('departure_info', $request->validated());
return $this->apiResponse(true,'Data Saved Successfully!',200,[]);
}
now if i print the session data in the same controller in the same api request. it works. However
if i try to get the session data in another controller like..
function request(CreateServiceRequest $request)
{
$data = Session::get('order_info', 'No order information found');
return $data;
$data = $this->formatService->formatData($request->validated());
}
it is not working.
i read some content online they said to put the api's under "web" middleware but then it give CSRF mis match error.
My routes are as follows
<?php
use App\Http\Controllers\Api\User\MovingServiceController;
use Illuminate\Support\Facades\Route;
Route::middleware(['auth:sanctum'])->prefix('user/service')->group(function () {
Route::post('order-info', [MovingServiceController::class, 'orderInfo']);
Route::post('departure', [MovingServiceController::class, 'departure']);
Route::post('request', [MovingServiceController::class, 'request']);
});
?>
<?php
use App\Http\Middleware\Admin;
use App\Http\Middleware\User;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Support\Facades\Route;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
commands: __DIR__.'/../routes/console.php',
using: function () {
Route::middleware('web')
->group(base_path('routes/web.php'));
Route::prefix('api/v1')
->group(base_path('routes/v1/api.php'));
},
)
->withMiddleware(function (Middleware $middleware) {
$middleware->alias([
'admin' => Admin::class,
'user' => User::class,
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middleware\PermissionMiddleware::class,
'role_or_permission' => \Spatie\Permission\Middleware\RoleOrPermissionMiddleware::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
$exceptions->renderable(function (\Spatie\Permission\Exceptions\UnauthorizedException $e)
{
return response()->json([
'success' => false,
'message' => "You don't have the appropriate role or permission",
'status' => 403,
],403);
});
})->create();