Laravel 4-打包还是“模块"? [英] Laravel 4 - Package or "modules"?

查看:71
本文介绍了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?

然后我发现了这个链接以启用类似模块的系统.那么,我应该采取什么方法以保持幼稚的方式?

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创建了Andreyco\Cart程序包.
包装及其结构

First, I created Andreyco\Cart 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)

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

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

问: 放置控制器和模型的位置
A:只需在其中创建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 Andreyco\Cart\Controllers;

use Andreyco\Cart\Models\TestModel;

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



// workbench/andreyco/cart/src/models/TestModel.php
<?php namespace Andreyco\Cart\Models;

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.

问: 那么,我应该采取什么方法以保持幼稚的方式?
A:我认为,您应该坚持使用软件包.至少,我会的.这就是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天全站免登陆