<small id='HAm1I'></small><noframes id='HAm1I'>

    <bdo id='HAm1I'></bdo><ul id='HAm1I'></ul>
  • <tfoot id='HAm1I'></tfoot>
  • <i id='HAm1I'><tr id='HAm1I'><dt id='HAm1I'><q id='HAm1I'><span id='HAm1I'><b id='HAm1I'><form id='HAm1I'><ins id='HAm1I'></ins><ul id='HAm1I'></ul><sub id='HAm1I'></sub></form><legend id='HAm1I'></legend><bdo id='HAm1I'><pre id='HAm1I'><center id='HAm1I'></center></pre></bdo></b><th id='HAm1I'></th></span></q></dt></tr></i><div id='HAm1I'><tfoot id='HAm1I'></tfoot><dl id='HAm1I'><fieldset id='HAm1I'></fieldset></dl></div>
    <legend id='HAm1I'><style id='HAm1I'><dir id='HAm1I'><q id='HAm1I'></q></dir></style></legend>

      1. 使用我自己的 Laravel API 时如何遵循不要重复自己的原则?

        How Follow the Don#39;t Repeat Yourself Principle When Consuming My Own Laravel API?(使用我自己的 Laravel API 时如何遵循不要重复自己的原则?)
            <tbody id='T2aQA'></tbody>

            • <bdo id='T2aQA'></bdo><ul id='T2aQA'></ul>

            • <i id='T2aQA'><tr id='T2aQA'><dt id='T2aQA'><q id='T2aQA'><span id='T2aQA'><b id='T2aQA'><form id='T2aQA'><ins id='T2aQA'></ins><ul id='T2aQA'></ul><sub id='T2aQA'></sub></form><legend id='T2aQA'></legend><bdo id='T2aQA'><pre id='T2aQA'><center id='T2aQA'></center></pre></bdo></b><th id='T2aQA'></th></span></q></dt></tr></i><div id='T2aQA'><tfoot id='T2aQA'></tfoot><dl id='T2aQA'><fieldset id='T2aQA'></fieldset></dl></div>

                  <small id='T2aQA'></small><noframes id='T2aQA'>

                1. <legend id='T2aQA'><style id='T2aQA'><dir id='T2aQA'><q id='T2aQA'></q></dir></style></legend>
                  <tfoot id='T2aQA'></tfoot>
                  本文介绍了使用我自己的 Laravel API 时如何遵循不要重复自己的原则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个 Laravel 4 应用程序,它将通过 JSON REST API 和 Web UI 对我的数据集进行相同的 CRUD 操作.似乎为了防止违反 DRY 原则,我的 UI 应该通过将所有请求从 UI 路由回 API 来使用我自己的 API.虽然我不确定完成这项工作的最佳方法.大概我会有单独的 UI 和 API 控制器,并以某种方式路由请求.还是我应该考虑完全不同的方法?

                  I'm developing a Laravel 4 app that will make the same CRUD operations on my data set available through a JSON REST API and a Web UI. It seems that to prevent breaking the DRY principle that my UI should consume my own API by routing all requests from the UI back to the API. I'm unsure though about the best approach to making this work. Presumably I would have separate UI and API controllers and somehow route the requests through. Or should I be looking at a different approach altogether?

                  推荐答案

                  我实际上正在修改相同的想法,它非常简洁.使用 Laravel,您确实可以发出内部请求(有些人可能将其称为 HMVC,但我不会).这是内部请求的基础知识.

                  I'm actually tinkering with the same idea and it's pretty neat. With Laravel you do have the ability to make internal requests (some might refer to this as HMVC, but I won't). Here's the basics of an internal request.

                  $request = Request::create('/api/users/1', 'GET');
                  
                  $response = Route::dispatch($request);
                  

                  $response 现在将包含 API 返回的响应.通常这将返回一个 JSON 编码的字符串,这对客户端来说非常有用,但对于内部 API 请求来说不是那么好.您必须在这里扩展一些东西,但基本上想法是通过内部调用返回实际对象,而外部请求返回格式化的 JSON 响应.您可以在此处使用 $response->getOriginalContent() 之类的东西.

                  $response will now contain the returned response of the API. Typically this will be returned a JSON encoded string which is great for clients, but not that great for an internal API request. You'll have to extend a few things here but basically the idea is to return the actual object back through for the internal call, and for external requests return the formatted JSON response. You can make use of things like $response->getOriginalContent() here for this kind of thing.

                  您应该关注的是构建某种内部 Dispatcher 允许您分派 API 请求并返回原始对象.调度程序还应处理格式错误的请求或错误响应并抛出异常以匹配.

                  What you should look at doing is constructing some sort of internal Dispatcher that allows you to dispatch API requests and return the original object. The dispatcher should also handle malformed requests or bad responses and throw exceptions to match.

                  这个想法本身是可靠的.但是规划 API 是一项艰巨的工作.我建议你写一份所有预期端点的好列表,并起草几个 API 版本,然后选择最好的一个.

                  The idea itself is solid. But planning an API is hard work. I'd recommend you write up a good list of all your expected endpoints and draft a couple of API versions then select the best one.

                  这篇关于使用我自己的 Laravel API 时如何遵循不要重复自己的原则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <tbody id='riapz'></tbody>

                  <small id='riapz'></small><noframes id='riapz'>

                2. <legend id='riapz'><style id='riapz'><dir id='riapz'><q id='riapz'></q></dir></style></legend>

                      <tfoot id='riapz'></tfoot>

                          <i id='riapz'><tr id='riapz'><dt id='riapz'><q id='riapz'><span id='riapz'><b id='riapz'><form id='riapz'><ins id='riapz'></ins><ul id='riapz'></ul><sub id='riapz'></sub></form><legend id='riapz'></legend><bdo id='riapz'><pre id='riapz'><center id='riapz'></center></pre></bdo></b><th id='riapz'></th></span></q></dt></tr></i><div id='riapz'><tfoot id='riapz'></tfoot><dl id='riapz'><fieldset id='riapz'></fieldset></dl></div>
                            <bdo id='riapz'></bdo><ul id='riapz'></ul>