简体   繁体   English

Laravel刀片动态目录

[英]Laravel blade dynamic directory

I am working on a modular structure such as below 我正在研究如下的模块化结构

modules
    module
        controllers
        models
        views

However I am struggling to figure out how to load views from a dynamic directory, for instance there could be 20 different modules each one loading views from their own directory, is this possible with the way blade renders templates at the moment? 但是,我在努力寻找如何从动态目录中加载视图的方法,例如,可能有20个不同的模块,每个模块都从其自己的目录中加载视图,目前刀片式渲染模板的方式是否可行?

Although you're asking about "modular" behaviour, as mentioned in the comments you may want to consider Service Providers and Packages instead. 尽管您在询问“模块化”行为,但是如评论中所述,您可能需要考虑服务提供商和程序包。 Laravel is more Service-Oriented Architecture . Laravel是更多面向服务的体系结构 As referenced you can learn more about SOA on Wikipedia. 作为参考,您可以在Wikipedia上了解有关SOA的更多信息。

IMHO modules are an old practice and are inflexible when it comes to dependencies - when two packages need to override the same dependency in a package but also not know of each other - which takes precedence? IMHO模块是一种古老的做法,当涉及到依赖关系时(两个包需要覆盖一个包中的相同依赖关系,但又彼此不了解)时,IMHO模块不灵活-哪个优先?

Creating a Package 创建一个包

In order for you to get started, the quickest way is to create your own package which will have it's own repository (which we assume will be GIT and a local repository - it can be anywhere however, private repository on GitHub, Bitbucket or even your own private server). 为了让您开始使用,最快的方法是创建自己的程序包,该程序包将拥有自己的存储库(我们假设它将是GIT和本地存储库-它可以在任何位置,但是可以在GitHub,Bitbucket甚至您的私有存储库中使用)自己的专用服务器)。

First create your repository, I will assume you're doing it locally inside /Users/developer/Projects . 首先创建您的存储库,我假设您在/Users/developer/Projects内部进行本地存储。

$ cd ~/Projects
$ git init blog

As per the Composer Documentation you will need a Composer config in order for your Application to update the package in vendor . 根据Composer文档,您将需要Composer配置,以便您的应用程序更新vendor的软件包。

You will want to create this inside your blog repository that has just been created (File: composer.json). 您将要在刚刚创建的blog存储库中创建此文件(文件:composer.json)。

{
    "name": "yourcompany/blog",
    "description": "Description of what the package is.",
    "version": "1.0.0-dev",
    "authors": [
        {
            "name": "Developer Name",
            "email": "developer@company",
            "role": "Software Develper"
        }
    ],
    "require": {
        "php": ">=5.5.9",
    },
    "autoload": {
        "psr-4": {
            "Company\\Blog\\": "src/"
        }
    },
    "minimum-stability": "dev"
}

So you package structure should look something like the following: 因此,您的程序包结构应类似于以下内容:

blog/
├── database/
    ├── migrations/
    ├── seeds/
├── resources/
    ├── assets/
    ├── lang/
    ├── views/
├── src/
    ├── BlogServiceProvider.php
├── composer.json
├── readme.md

The only mandatory files/directories here are the composer.json file and the src directory. 这里唯一的必需文件/目录是composer.json文件和src目录。

If you refer to the Laravel Documentation - Package Development You will quickly learn how to set-up Service Providers, Route Service Providers and make your package publishable. 如果您参考Laravel文档-程序包开发,您将快速学习如何设置服务提供者,路由服务提供者并使包可发布。 This is what you're trying to achieve? 这是您要达到的目标?

When you publish content into the application, it allows you to override views in your application and keep your package generic without hacking things together. 当您将内容发布到应用程序中时,它允许您覆盖应用程序中的视图并保持程序包的通用性,而不会相互干扰。

Register a package 注册包裹

You now want to register your package with your application, simply do this by editing your application/project composer.json . 现在,您想在应用程序中注册包,只需通过编辑应用程序/项目composer.json

As your packages are private you will need to tell composer where your repositories are. 由于您的软件包是私有的,因此您需要告诉作曲者您的存储库在哪里。 After the "type": "project", config you need to define the repositories; "type": "project",需要定义配置库; something like: 就像是:

"repositories": [
    {
        "type": "vcs",
        "url": "/Users/developer/Projects/blog"
    }
]

You need to inform composer you require your blog package, the repositories simply defines its location when Composer fails to discover it on packagist. 您需要通知作曲家您需要您的博客包,当作曲家未能在packagist上发现它时,存储库仅定义其位置。

"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~4.0",
    "phpspec/phpspec": "~2.1",
    "vendor/blog": "dev-master"
},

However, once you've completed development on your package you should move it into the "require" list and not the "require-dev" also replacing the version: 但是,一旦完成软件包的开发,就应将其移至“ require”列表中,而不是“ require-dev”也要替换该版本:

"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.*",
    "vendor/blog": "1.0.*"
},

Code examples 代码示例

You can review the Laravel Source in order to build your package. 您可以查看Laravel Source来构建您的软件包。


To conclude; 结论 a package functions like a module except there are many more advantages to packages all of which are listed as part of the SOLID pattern and there is a learning curve with it. 程序包的功能类似于模块,除了程序包具有更多优势外,所有这些都列为SOLID模式的一部分,并且学习过程也很轻松

Checkout pingpong/modules : http://sky.pingpong-labs.com/docs/2.1/modules . 检出pingpong/moduleshttp : //sky.pingpong-labs.com/docs/2.1/modules The package does what you're trying to do and a lot more. 该软件包可以完成您想要做的事情,还有更多。

To load a view from a specific module (using the above package) you do it like this: 要从特定模块加载视图(使用上述程序包),您需要这样做:

view('moduleName:view-name')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM