From e5b0e1fcfd16902ca822d10256c39cef4eeb8b4f Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 9 Oct 2018 22:26:01 +0200 Subject: [PATCH] Make the application modular to have a "main" and "backend" part. --- app/config/app.yml | 3 +- app/config/routes.yml | 5 ++ app/controllers/backend/UserController.php | 10 ++++ app/library/Bootstrap.php | 19 +++++++- app/library/Services.php | 3 +- app/modules/Backend.php | 12 +++++ app/modules/Base.php | 47 +++++++++++++++++++ app/modules/Main.php | 12 +++++ .../_components}/content-section.volt | 0 .../_components}/flash.volt | 0 .../_components}/footer.volt | 0 .../_components}/navigation.volt | 0 .../{ => _common}/_partials/pagination.volt | 0 app/views/{ => _common}/layout-front.volt | 0 app/views/{ => _common}/layout.volt | 6 +-- app/views/backend/user/index.volt | 2 + app/views/{ => main}/auth/index.volt | 0 app/views/{ => main}/auth/register.volt | 0 app/views/{ => main}/callback/created.volt | 0 app/views/{ => main}/callback/list.volt | 0 app/views/{ => main}/callback/new.volt | 0 app/views/{ => main}/callback/show.volt | 0 app/views/{ => main}/error/error.volt | 0 app/views/{ => main}/error/show404.volt | 0 app/views/{ => main}/index/about.volt | 0 app/views/{ => main}/index/index.volt | 0 app/views/{ => main}/user/activity.volt | 0 app/views/{ => main}/user/settings.volt | 0 28 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 app/controllers/backend/UserController.php create mode 100644 app/modules/Backend.php create mode 100644 app/modules/Base.php create mode 100644 app/modules/Main.php rename app/views/{_templates => _common/_components}/content-section.volt (100%) rename app/views/{_templates => _common/_components}/flash.volt (100%) rename app/views/{_templates => _common/_components}/footer.volt (100%) rename app/views/{_templates => _common/_components}/navigation.volt (100%) rename app/views/{ => _common}/_partials/pagination.volt (100%) rename app/views/{ => _common}/layout-front.volt (100%) rename app/views/{ => _common}/layout.volt (78%) create mode 100644 app/views/backend/user/index.volt rename app/views/{ => main}/auth/index.volt (100%) rename app/views/{ => main}/auth/register.volt (100%) rename app/views/{ => main}/callback/created.volt (100%) rename app/views/{ => main}/callback/list.volt (100%) rename app/views/{ => main}/callback/new.volt (100%) rename app/views/{ => main}/callback/show.volt (100%) rename app/views/{ => main}/error/error.volt (100%) rename app/views/{ => main}/error/show404.volt (100%) rename app/views/{ => main}/index/about.volt (100%) rename app/views/{ => main}/index/index.volt (100%) rename app/views/{ => main}/user/activity.volt (100%) rename app/views/{ => main}/user/settings.volt (100%) diff --git a/app/config/app.yml b/app/config/app.yml index 398b1cf..a486196 100644 --- a/app/config/app.yml +++ b/app/config/app.yml @@ -1,9 +1,10 @@ application: + modulesDir : ../app/modules/ controllersDir : ../app/controllers/ modelsDir : ../app/models/ migrationsDir : ../app/migrations/ - viewsDir : ../app/views/ + viewsDir : ../app/views/_common/ templateDir : ../app/templates/ listenersDir : ../app/listeners/ libraryDir : ../app/library/ diff --git a/app/config/routes.yml b/app/config/routes.yml index 7b0f625..aba0eda 100644 --- a/app/config/routes.yml +++ b/app/config/routes.yml @@ -56,3 +56,8 @@ router: path: controller: api action: activationlink + + # Backend + backend-home: + pattern: '/admin' + path: backend::user::index diff --git a/app/controllers/backend/UserController.php b/app/controllers/backend/UserController.php new file mode 100644 index 0000000..88a4e4f --- /dev/null +++ b/app/controllers/backend/UserController.php @@ -0,0 +1,10 @@ +setDI($di); + + $this->_app = new Application(); } /** @@ -34,6 +41,14 @@ class Bootstrap extends Injectable $di->get('debugger')->listen(true, true); } + // Modules + $this->_app->registerModules([ + 'main' => [ 'className' => 'App\Module\Main' ], + 'backend' => [ 'className' => 'App\Module\Backend' ], + ]); + + $this->_app->setDefaultModule('main'); + return $this; } @@ -44,7 +59,7 @@ class Bootstrap extends Injectable */ public function run() { - $app = new Application($this->getDI()); - return $app->handle()->getContent(); + $this->_app->setDI($this->getDI()); + return $this->_app->handle()->getContent(); } } diff --git a/app/library/Services.php b/app/library/Services.php index af06def..c6b8ef3 100644 --- a/app/library/Services.php +++ b/app/library/Services.php @@ -99,6 +99,7 @@ class Services extends DiDefault $loader->registerNamespaces(array( 'App\Controller' => $config->application->controllersDir, 'App\Listener' => $config->application->listenersDir, + 'App\Module' => $config->application->modulesDir, 'App\Model' => $config->application->modelsDir, 'App\Form' => $config->application->formsDir, 'Httpcb' => $config->application->libraryDir, @@ -222,7 +223,7 @@ class Services extends DiDefault $view = new View(); - $view->setViewsDir($config->application->viewsDir); + $view->setViewsDir([ $config->application->viewsDir ]); $view->setLayoutsDir('_layouts/'); $view->setPartialsDir('_partials/'); diff --git a/app/modules/Backend.php b/app/modules/Backend.php new file mode 100644 index 0000000..aa1ca6e --- /dev/null +++ b/app/modules/Backend.php @@ -0,0 +1,12 @@ +registerNamespaces([ + $this->_controllerNamespace => $this->_controllerPath, + ]); + + $loader->register(); + } + + /** + * Register specific services for the module + */ + public function registerServices(DiInterface $di) + { + $dispatcher = $di->get('dispatcher'); + + $dispatcher->setDefaultNamespace($this->_controllerNamespace); + + $di->get('view')->setViewsDir(array_merge( + [ $this->_viewDir ], + (array) $di->get('view')->getViewsDir() + )); + } +} + diff --git a/app/modules/Main.php b/app/modules/Main.php new file mode 100644 index 0000000..5e397b0 --- /dev/null +++ b/app/modules/Main.php @@ -0,0 +1,12 @@ +
- {% include "_templates/navigation.volt" %} + {% include "_components/navigation.volt" %}
{% block masthead %}{% endblock %} @@ -20,14 +20,14 @@
- {% include "_templates/flash.volt" %} + {% include "_components/flash.volt" %} {{ content() }}
{{ assets.outputJs() }} diff --git a/app/views/backend/user/index.volt b/app/views/backend/user/index.volt new file mode 100644 index 0000000..f12e351 --- /dev/null +++ b/app/views/backend/user/index.volt @@ -0,0 +1,2 @@ + +

Backend

diff --git a/app/views/auth/index.volt b/app/views/main/auth/index.volt similarity index 100% rename from app/views/auth/index.volt rename to app/views/main/auth/index.volt diff --git a/app/views/auth/register.volt b/app/views/main/auth/register.volt similarity index 100% rename from app/views/auth/register.volt rename to app/views/main/auth/register.volt diff --git a/app/views/callback/created.volt b/app/views/main/callback/created.volt similarity index 100% rename from app/views/callback/created.volt rename to app/views/main/callback/created.volt diff --git a/app/views/callback/list.volt b/app/views/main/callback/list.volt similarity index 100% rename from app/views/callback/list.volt rename to app/views/main/callback/list.volt diff --git a/app/views/callback/new.volt b/app/views/main/callback/new.volt similarity index 100% rename from app/views/callback/new.volt rename to app/views/main/callback/new.volt diff --git a/app/views/callback/show.volt b/app/views/main/callback/show.volt similarity index 100% rename from app/views/callback/show.volt rename to app/views/main/callback/show.volt diff --git a/app/views/error/error.volt b/app/views/main/error/error.volt similarity index 100% rename from app/views/error/error.volt rename to app/views/main/error/error.volt diff --git a/app/views/error/show404.volt b/app/views/main/error/show404.volt similarity index 100% rename from app/views/error/show404.volt rename to app/views/main/error/show404.volt diff --git a/app/views/index/about.volt b/app/views/main/index/about.volt similarity index 100% rename from app/views/index/about.volt rename to app/views/main/index/about.volt diff --git a/app/views/index/index.volt b/app/views/main/index/index.volt similarity index 100% rename from app/views/index/index.volt rename to app/views/main/index/index.volt diff --git a/app/views/user/activity.volt b/app/views/main/user/activity.volt similarity index 100% rename from app/views/user/activity.volt rename to app/views/main/user/activity.volt diff --git a/app/views/user/settings.volt b/app/views/main/user/settings.volt similarity index 100% rename from app/views/user/settings.volt rename to app/views/main/user/settings.volt