Laravel + Vue:在web.php/api.php上的路由 [英] Laravel + Vue : Routes on web.php / api.php

查看:51
本文介绍了Laravel + Vue:在web.php/api.php上的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的一个新手,我已经开始从事一个项目,我想就如何选择好的架构获得一些建议.

I am quite a new guy on Laravel and I have started working on a project, and I would like get some advice about the good architecture to pick.

我决定通过Vue管理前台,并通过Laravel管理后端.

I decided to manage my front with Vue and my backend with Laravel.

我开始做的是捕获通过Laravel的路由,并将其发送到我的Vue路由,以确定要加载的组件等.我当时正在考虑通过axios向我的Vue.components中进行"api"调用以进行检索从我的控制器中获取的数据(JSON格式),以便将其显示在我的视图中.

What I have started to do is to capture the routes through Laravel and sent it to my Vue routes to determines which components to load etc. And I was thinking to make "api" calls though axios into my Vue.components to retrieve the data (in JSON format) from my controller in order to display them into my view.

那仍然是一个网络"需求.

That's still a "web" needs.

但是我也在考虑我将提供的未来API.

But I am also thinking to the future API I will provide.

那最好的选择是什么?

将一些 Route :: resource('model','Controller')放入 web.php 文件,然后在我的Vue.components中调用这些路由,然后将 Route :: resource('model','Controller')放入我的 api.php 文件中?

Put some Route::resource('model', 'Controller') into the web.php file, then call those routes in my Vue.components, and put the Route::resource('model','Controller') into my api.php file ?

谢谢您的回答,我不知道我是否走对了.

Thank you for your answer I don't know if I am going the right way or not.

Clément.

推荐答案

如果您查看 app \ Http \ Kernel.php ,您会注意到网络和api组之间的区别.

If you look at app\Http\Kernel.php you'll notice the distinction between the web and the api group.

  • 网络-处理标准"请求-他们需要一个会话,进行检查针对在应用程序中全局登录的用户,从而处理网络要求.Web UI内容
  • api-处理无状态API请求,很可能总是使用并返回json

我通常将API路由保留在api.php中,为它添加一个版本前缀,然后从API名称空间中加载控制器.这使它更具可维护性,因为v1方法可以通过潜在的v2版本进行扩展

I usually keep the API routes in the api.php, prefix it with a version and load the controllers from the API namespace. This makes it a little more maintainable because v1 methods can be extended by a potential v2 version

Route::prefix('v1')->namespace('Api\V1')->group(function () {
    // ... API routes
});

这篇关于Laravel + Vue:在web.php/api.php上的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆