Laravel 4 - 包或“模块"? [英] Laravel 4 - Package or "modules"?

查看:10
本文介绍了Laravel 4 - 包或“模块"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 Laravel 4,这似乎是一个不错的选择,无论如何,在编写我的第一个项目之前,我想了解我通常的方法是否适用于 laravel.

I'm starting to work with Laravel 4, it seems to be a great choice, anyway before coding my first project i'd like to understand if my usual approach could be ok with laravel.

通常我将后端和前端的三元组分隔在/modules 文件夹下,如下所示:

Usually i keep triad for backend and frontend separated under a /modules folder, like this:

/modules
       /backend
               /config
               /controllers
               /models
               /migrations
               /ecc..
       /frontend (and so on...)

对于 laravel,我不太确定如何管理它.我正在尝试使用包,但 php artisan workbench me/mypackage --resources 并没有构建整个文件夹结构......在哪里放置控制器和模型,以及如何设置路由?

With laravel i'm not really sure how to manage this. I'm trying with packages, but the php artisan workbench me/mypackage --resources don't build the entire folder structure... where to put controllers and models, and how to setup routes?

然后我找到了这个 链接 来启用类似模块的系统.那么,我应该遵循什么方法来保持 laravel 的方式?

Then i found this link to enable modules-like system. So, what's the approach i should follow for keeping things in the laravel way?

推荐答案

好吧,让我们开始吧……

Well, let's begin...

首先,我使用 Artisan 创建了 AndreycoCart 包.
包及其结构

First, I created AndreycoCart package using Artisan.
Package and it's structure

|workbench
|-andreyco
|---cart
|-----public
|-----src
|-------Andreyco
|---------Cart
|-------config
|-------lang
|-------migrations
|-------views
|-----tests
|-----vendor

在答案中,我将使用这个确切的包作为示例.

In the answers, I will use this exact package as example.

想象一下,那个文件夹 workbench/andreyco/cart/src 是应用程序文件夹.如果你这样做了,你应该知道大部分的答案.(其实App也是包)

Imagine, that folder workbench/andreyco/cart/src is the application folder. If you do, you should know the most of the answers. (Actually App is package as well)

问: 如何设置路线
答: 创建文件 -> workbench/andreyco/cart/src/routes.php.这样就完成了.

Q: how to setup routes
A: Create the file -> workbench/andreyco/cart/src/routes.php. This is done.

问: 控制器和模型的放置位置
答: 只需在此处创建 controllersmodels 文件夹即可.
因此 TestController 将位于 workbench/andreyco/cart/src/controllers/TestController.php 文件中.与模型非常相似.
目录树看起来像这样

Q: where to put controllers and models
A: Just create controllers and models folder there.
So the TestController would be located at workbench/andreyco/cart/src/controllers/TestController.php file. Very same with models.
Directory tree would look like this

|workbench
|-andreyco
|---cart
|-----public
|-----src
|-------Andreyco
|---------Cart
|-------config
|-------controlers
|-------lang
|-------migrations
|-------models
|-------views
|-----tests
|-----vendor

我创建了 routes.phpTestController.phpTestModel.php

// workbench/andreyco/cart/src/routes.php
<?php

Route::get('test', 'Andreyco\Cart\Controllers\TestController@index');



// workbench/andreyco/cart/src/controllers/TestController.php
<?php namespace AndreycoCartControllers;

use AndreycoCartModelsTestModel;

class TestController extends BaseController
{
    public function index()
    {
        return TestModel::printCurrentLocation(__DIR__);
    }
}



// workbench/andreyco/cart/src/models/TestModel.php
<?php namespace AndreycoCartModels;

class TestModel extends Eloquent
{
    public static function printCurrentLocation($location)
    {
        return "Testing package controller, script is located at: {$location}";
    }
}

如您所见,我使用了命名空间,所以您应该这样做.
命名空间让您的生活更轻松.

As you can see, I used namespaces, so you should.
Namespaces make your life a lot of easier.

重要提示:创建这些文件后,您需要更新composer.json文件,以便自动加载类

Important: after creating those files, you need to update composer.json file, so that classes could be autoloaded

// composer.json
"autoload": {
    "classmap": [
        ...
        "workbench/andreyco/cart/src/controllers",
        "workbench/andreyco/cart/src/models"
    ]
},

在此之后,使用 composer dump-autoload -o 命令转储更改.

After this, dump the changes using composer dump-autoload -o command.

问: 那么,我应该遵循什么方法来以 laravel 的方式保存东西?
答:在我看来,你应该坚持使用包.至少,我愿意.这就是 Laravel 的工作方式.

Q: So, what's the approach i should follow for keeping things in the laravel way?
A: In my opinion, you should stick to packages. At least, I would. That's the way Laravel was designed to work.

希望对你有帮助,祝你好运!

I hope this helps you, good luck!

编辑
这里的观点没有问题.它们就像在主应用程序包中一样工作.

Edit
Views are not problem here. They work just like in main app package.

// workbench/cart/src/view/foldername/viewname.blade.php

<h1>Testing view file.</h1>
{{ "Blade" }} syntax is parsed as well, no problem here.

从包的控制器返回视图非常简单

Returning view from package's controller is pretty simple

public function index()
    {
        return View::make('cart::foldername.viewname');
    }

这篇关于Laravel 4 - 包或“模块"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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