'Pass value from vuejs view once and use common for all controllers in laravel?
After logging in, I let it go to the home page to choose to access one of the two tools:
<button @click="checkTool(1)" class="btn btn-lg mr-2">Tool 1</button>
<button @click="checkTool(2)" class="btn btn-lg">Tool 2</button>
methods: {
checkTool(id) {
localStorage.setItem("idTool", id);
this.$router.push({ path: "/dashboard" });
}
}
When the user selects the tool, I will set the idTool to localStorage
.
And go to the dashboard
page.
In laravel i have 4 controller :
UserController.php
<?php
namespace App\Http\Controllers;
class UserController extends Controller
{
}
ProductController.php
<?php
namespace App\Http\Controllers;
class ProductController extends Controller
{
}
TicketController.php
<?php
namespace App\Http\Controllers;
class TicketCodeController extends Controller
{
}
Normally I would give it a route api and then use axios
to pass the value from the view to the controller and use $request
to get that value out.
Now I want to get that idTool
through the controller to check if it has permission under that idTool
, actually this is very easy..But the problem here is that I don't want to have to pass idTool
in every api call to check..Is there a way to get idTool
and use it for all controllers
? I have researched about Interface
but still can't solve the problem. Please give me your opinion.Thank you
Solution 1:[1]
You can set a session with idTool value on backend-laravel and check it as a middleware for every request. This is first option I can suggest at the moment.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Farshid Ghiasimanesh |