Laravel 模块

问题描述 投票:0回答:2

我带来了这个模块解决方案,我想知道它是否有什么问题?

作曲家.json

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

应用程序/配置/app.php

'provider' => array(
    '....',
    'Modules\ServiceProvider\ModulesServiceProvider'

应用程序/模块/ModulesServiceProvider.php

    <?php

namespace Modules\ServiceProvider;

use Illuminate\Support\ServiceProvider;

class ModulesServiceProvider extends ServiceProvider
{
    public function register() {
        $this->app->bind('modules', function()
        {
            return new Modules;
        });
    }

    public function boot() {

        // set modules path
        $modules_path = __DIR__ . '/';
        // scan modules directory
        $modules = scandir($modules_path);

        foreach($modules as $module)
        {
            if($module === '.' || $module === '..') continue;
            // check if module exist
            if(is_dir($modules_path) . '/' . $module)
            {
                // set routes.php path
                $routes_path = $modules_path . $module . '/routes.php';
                            // set modules views path
                $views_path = $modules_path . $module . '/views';
                // if routes.php exists
                if(file_exists($routes_path))
                {
                    // required routes.php
                    \View::addNamespace($module, $views_path);
                    require_once($routes_path);
                }
                else
                {
                    // else do ... what ??
                    continue;
                }
            }
        }
    }
}

一切正常,我可以使用 View::make('moduleName::viewName') 调用视图,每个模块中都定义了路由,这使得维护更容易。

如果这不是一个好的解决方案,您能解释一下吗?

php laravel module
2个回答
0
投票

这是我在我的包中的做法l5-modular

protected $files;

/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot() {
    if(is_dir(app_path().'/Modules/')) {
        $modules = array_map('class_basename', $this->files->directories(app_path().'/Modules/'));
        foreach($modules as $module)  {

            $routes = app_path().'/Modules/'.$module.'/routes.php';
            $views  = app_path().'/Modules/'.$module.'/Views';
            $trans  = app_path().'/Modules/'.$module.'/Translations';
            if($this->files->exists($routes)) include $routes;
            if($this->files->isDirectory($views)) $this->loadViewsFrom($views, $module);
            if($this->files->isDirectory($trans)) $this->loadTranslationsFrom($trans, $module);
        }
    }
}

0
投票
modules -> Database -> migration , Provider -> routeserviceProvider ,models , 
          Controller 

autoload  :   "Modules\\": "modules/",
            "Modules\\Product\\": "modules/Product/src"

 "ide": [
            "@php artisan ide-helper:models --dir='modules'"
        ]

art ide-helper:models --dir='modules'

composer run ide

namespace Modules\Order\Infrastructure\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class OrderServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
        $this->mergeConfigFrom(__DIR__.'/../config.php', 'order');

        $this->app->register(RouteServiceProvider::class);
        $this->app->register(EventServiceProvider::class);

        $this->loadViewsFrom(__DIR__ . '/../../Ui/Views', 'order');

        Blade::anonymousComponentPath(__DIR__ . '/../../Ui/Views/components', 'order');
        Blade::componentNamespace('Modules\\Order\\Ui\\ViewComponents', 'order');
    }
}

//RouteServiceProvider.php
namespace Modules\Shipment\Providers;

use Illuminate\Foundation\Support\Providers\RouteServiceProvider as BaseRouteServiceProvider;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends BaseRouteServiceProvider
{
    public function boot(): void
    {
        $this->routes(function () {
            Route::middleware('web')
                ->group(__DIR__.'/../routes.php');
        });
    }
}





//


    <?php
    
    namespace App\Http\Requests\Api\V1;
    
    use Illuminate\Foundation\Http\FormRequest;
    
    class BaseTicketRequest extends FormRequest
    {
        public function mappedAttributes(array $otherAttributes = []) {
            $attributeMap = array_merge([
                'data.attributes.title' => 'title',
                'data.attributes.description' => 'description',
                'data.attributes.status' => 'status',
                'data.attributes.createdAt' => 'created_at',
                'data.attributes.updatedAt' => 'updated_at',
                'data.relationships.author.data.id' => 'user_id',
            ], $otherAttributes);
    
            $attributesToUpdate = [];
            foreach ($attributeMap as $key => $attribute) {
                if ($this->has($key)) {
                    $attributesToUpdate[$attribute] = $this->input($key);
                }
            }
    
            return $attributesToUpdate;
        }
    
        public function messages() {
            return [
                'data.attributes.status' => 'The data.attributes.status value is invalid. Please use A, C, H, or X.'
            ];
        }
    }
    
    
    //controller
      $ticket->update($request->mappedAttributes());
© www.soinside.com 2019 - 2024. All rights reserved.