如何在Laravel 5中构建模块化应用程序? [英] How to structure a modular app in Laravel 5?

查看:211
本文介绍了如何在Laravel 5中构建模块化应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的应用程序划分为模块.例如,将有一个核心"模块,其中包含基本的登录功能,应用程序布局/格式(CSS等),用户管理和日记.

I would like to divide my application in modules. For instance, there would be a "core" modules that contains the basic login functionality, app layout/formatting (CSS etc), user management and a diary.

稍后,我可能会创建其他模块,例如联系人管理器,可以轻松地从应用程序中添加或删除该模块.

Later on I may create other modules like a contact manager that can easily be added or removed from the application.

应用程序导航中将存在一些逻辑,用于确定存在哪些模块并显示/隐藏到它们的链接.

There would be some logic in the apps navigation for determining which modules are present and to show/hide the links to them.

我该如何从目录结构,名称空间和其他任何需要的角度做到这一点?

How can I do this in terms of directory structure, namespaces and anything else that's needed?

我正在查看creolab/laravel-modules,但它指出它适用于Laravel4.我是否仍可以以完全相同的方式将它与5一起使用?

I am looking at creolab/laravel-modules but it states that it is for Laravel 4. Can I still use it with 5 in exactly the same way?

文档说要在每个模块目录中放置模型,控制器和视图,但是这如何与路径一起使用?理想情况下,我希望每个模块都具有自己的route.php文件.所有这些如何与httpresources目录中的内容一起使用?

The documentation says to place models, controllers and views within each module directory, but how does this work with routes? Ideally I would like each module to have its own routes.php file. How will all of this work with the stuff in the http and the resources directory?

我在想这样的事情:

但是我不知道如何使它工作.

But I have no idea how I would get it to work.

我刚刚在这里尝试了本教程:

I have just tried the tutorial here:

http://creolab.hr/2013/05/modules-in-laravel-4/

无需额外的库等,只需使用纯Laravel 5.

With no extra libraries etc, just pure Laravel 5.

我似乎遇到了一条错误消息:

I seem to have hit a brick wall with an error message:

FatalErrorException in ServiceProvider.php line 16:
Call to undefined method Illuminate\Config\Repository::package()

关于以下内容:

<?php namespace App\Modules;

abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider
{

    public function boot()
    {
        if ($module = $this->getModule(func_get_args())) {
            $this->package('app/' . $module, $module, app_path() . '/modules/' . $module);
        }
    }

    public function register()
    {
        if ($module = $this->getModule(func_get_args())) {
            $this->app['config']->package('app/' . $module, app_path() . '/modules/' . $module . '/config');

// Add routes
            $routes = app_path() . '/modules/' . $module . '/routes.php';
            if (file_exists($routes)) require $routes;
        }
    }

    public function getModule($args)
    {
        $module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;

        return $module;
    }

}

是什么原因造成的,我该如何解决?

What is causing this and how can I fix it?

现在我可以解决这个问题.得到了我的程序包/模块的路由和视图,这很棒:

Got my head around this a bit more now. Got my package/module routes and views working which is great:

abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider
{

    public function boot()
    {
        if ($module = $this->getModule(func_get_args())) {
            include __DIR__.'/'.$module.'/routes.php';
        }
        $this->loadViewsFrom(__DIR__.'/'.$module.'/Views', 'core');
    }

    public function register()
    {
        if ($module = $this->getModule(func_get_args())) {

        }
    }

    public function getModule($args)
    {
        $module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null;

        return $module;
    }

}

我还有最后一个问题,如何从包内部加载所有控制器,就像loadViewsFrom()方法的工作原理一样?

I have one last question, how would I load all my controllers from inside my package, much like how the loadViewsFrom() method works?

推荐答案

我似乎已经弄清楚了.

我将其张贴在这里,以防它对其他初学者有所帮助,这只是关于正确命名空间的问题.

I'll post it here in case it helps other beginners, it was just about getting the namespaces right.

在我的composer.json中,我有:

In my composer.json I have:

...
"autoload": {
    "classmap": [
        "database",
        "app/Modules"
    ],
    "psr-4": {
        "App\\": "app/",
        "Modules\\": "Modules/"
    }
}

我的目录和文件最终像这样:

My directory and files ended up like this:

我通过将用于该模块的控制器包装在指定名称空间的组中来使我的核心模块router.php工作:

I got my Core module router.php to work by wrapping my controllers for that module in a group specifying the namespace:

Route::group(array('namespace' => 'Modules\Core'), function() {
    Route::get('/test', ['uses' => 'TestController@index']);
});

我想当我要为程序包建立模型时,将是正确使用命名空间的类似情况.

I imagine when I come to doing my models for the package it will be a similar case of getting the namespaces right.

感谢您的帮助和耐心!

这篇关于如何在Laravel 5中构建模块化应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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