简体   繁体   中英

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?

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 . As referenced you can learn more about SOA on Wikipedia.

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?

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).

First create your repository, I will assume you're doing it locally inside /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 .

You will want to create this inside your blog repository that has just been created (File: 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.

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. 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 .

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; 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.

"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": {
    "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.


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.

Checkout pingpong/modules : http://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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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