I am creating a project with vue+larave; approach. It is advisable to make API in laravel to send and collect data from vue components. But I am thinking about using traditional routes to deal with this because I want to work withweb:auth middleware and use user session to control any data flow.
Is the following approach appropriate? I want to go with the following approach but almost all videos and courses on internet shows vue + laravel example by making api in backend. So I was wondering if there are some points that I am missing..
/**
*
* Routes that will provide data to vue components
* Routes that will take data from vue components and save in database
*
*/
Route::get('/users','UserController@index');
Route::post('/create-user','UserController@create');
Route::post('/create-role','UserRoleController@create');
/**
*
* Any other routes will be be given to vue router
*/
Route::get('{any}',function (){
return view('app');
})->where('any','.*');
You can use the web.php for api routes (with sessions). I have done this myself in the past and it works just fine (spa with react). Same approach as your posted code
@martinbean I am wanting call to the web routes from vue components using CSRF token. The token will ensure that the request is coming from my own domain and the Laravel authentication will ensure that the user is logged in.
The alternative I have is to create API routes and then setup passport authentication and then use tokens to see which user is making this request and so on. I am trying to avoid that work for two reasons. One, because the API is never going to be given to anyone, so I don't want to work a lot on that and second is the development speed.
But of course, I do not want to go with my approach if it could cost me a hard time. That's why I created this thread to know if there is anything to be concerned about if I do it my way.
@ravish But you’re trying to mix two completely different approaches to building applications. Pick one, and go with it.
If time’s a factor, then just build a server-side-rendered application so you’re not having to debug JavaScript code as well as PHP code.
People seem to want to overcomplicate building websites these days. Just have a controller fetch data from your database and render it in a Blade template. Easy.
@ravish if you wish to use this solution and work really fast, you could consider trying out inertia js. It works by combining Vue and laravel to make a page that in theory is a single page app that is build as a regular app (laravel routing)
@martinbean You are right martinbean, there is no need to complicate things. I am encountering many issues with this approach.
@sinnbeck Thank you so much ! This is exactly what I was looking for. I can use VUE components for front end and server side routing and authentication without needing to create separate API.