Laravel 中的后端/前端分离

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21882662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:03:52  来源:igfitidea点击:

backend/frontend separation in laravel

phplaravellaravel-4

提问by Dexture

I come from a Codeignitor background. At the moment I am building a CMS in Laravel.

我来自 Codeignitor 背景。目前我正在 Laravel 中构建一个 CMS。

What I would like to know is how can I separate the backend and frontend in Laravel?

我想知道的是如何在 Laravel 中分离后端和前端?

In Codeignitor i use to make two controller Admin_Controller and Front_Controller.

在 Codeignitor 中,我用来制作两个控制器 Admin_Controller 和 Front_Controller。

Article extends Admin_Controller
Article extends Front_Controller

and the file structure looked like this

和文件结构看起来像这样

controller
--admin
---user
---blog
---news
--user 
--blog
--news

for admin controller I make separate folder and front end controller remain in root of controller folder.

对于管理控制器,我将单独的文件夹和前端控制器保留在控制器文件夹的根目录中。

Should I use the same logic in Laravel or is there a better way to do it?

我应该在 Laravel 中使用相同的逻辑还是有更好的方法来做到这一点?

回答by Antonio Carlos Ribeiro

If you want to create thinks like Taylor Otwell and 'the core' is trying to teach people do things in Laravel, this is a good start:

如果你想创造像 Taylor Otwell 这样的想法,并且“核心”是试图教人们在 Laravel 中做事,这是一个好的开始:

Your files could be organized as

您的文件可以组织为

├── app
│?? ├── ZIP
│?? │?? ├── Controllers
│?? │?? │?? ├── Admin
│?? │?? │?? │?? ├── Base.php <--- your base controller
│?? │?? │?? │?? ├── User.php
│?? │?? │?? │?? ├── Blog.php
│?? │?? │?? │?? ├── News.php
│?? │?? │?? ├── Front
│?? │?? │?? │?? ├── Base.php <--- your base controller
│?? │?? │?? │?? ├── User.php
│?? │?? │?? │?? ├── Blog.php
│?? │?? │?? │?? ├── News.php

Configure a PSR-0 or PSR-4 (better) to autoload your classes:

配置 PSR-0 或 PSR-4(更好)以自动加载您的类:

"psr-0": {
    "ZIP": "app/"
},

Create namespaces to all tour classes, according to your source tree:

根据您的源代码树,为所有游览类创建命名空间:

<?php namespace ZIP\Controllers\Admin

class User extends Base {

}


<?php namespace ZIP\Controllers\Front

class Blog extends Base {

}

And create your base controllers

并创建您的基本控制器

<?php namespace ZIP\Controllers\Admin

use Controller;

class Base extends Controller {

}

回答by Gaz_Edge

You can certainly do it the two controllers way or if you like even more separation (and a more 'laravel' way), write your front end and backend as separate packages (previously called bundles in Laravel 3).

你当然可以用两个控制器的方式来做,或者如果你喜欢更多的分离(和更“laravel”的方式),把你的前端和后端写成单独的包(以前在 Laravel 3 中称为包)。

They basically behave like standalone applications within your main app. They can have their own routes, models, controllers etc. You can also write 'core code' at the main application level which can be shared across the packages.

它们基本上就像主应用程序中的独立应用程序一样。它们可以有自己的路由、模型、控制器等。您还可以在主应用程序级别编写“核心代码”,这些代码可以在包之间共享。

If you are moving to Laravel as you want to learn a new framework, then you should definitely try and get a handle on packages - very powerful.

如果您因为想学习一个新框架而转向 Laravel,那么您绝对应该尝试处理包 - 非常强大。

If you are being 'made' to move to Laravel, or have some time pressure, just do it as you have normally done. Laravel is flexible and will be fine either way you do it.

如果您“被迫”迁移到 Laravel,或者有一些时间压力,请像往常一样去做。Laravel 很灵活,无论你怎么做都可以。

For more info, see the docs.

有关更多信息,请参阅文档。

Laravel current version (4 at time of writing) - http://laravel.com/docs/packages

Laravel 当前版本(撰写本文时为 4) - http://laravel.com/docs/packages

Laravel 3 - http://three.laravel.com/docs/bundles

Laravel 3 - http://three.laravel.com/docs/bundles