{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"Module","type":"basic","slug":"mvc-module","excerpt":"","body":"# Module\n**<a href=\"http://google.com/search?q=what+is+module\" target=\"_blank\">What is module all about?</a>**\nIn real world scenarios, we're building applications that communicates or uses the same resources, most probably you're building a large web application that must be divided into a different module, such as ``admin``, ``white label``, ``social network manager``, ``online payment manager`` and anything you think you must separate it from your current module.\n\n\n## Index:\n- [Basic Usage](#basic-usage)\n- [Alternative as a Service](#alternative-service)\n\n\n<a name=\"basic-usage\"></a>\n# Basic Usage\n\nLet us say, we want an ``admin`` module.\n\nRun this to your console:\n```\nphp brood app:module admin\n```\n\nThere must be generated directory and files, base it to these locations:\n- project-name/app/admin/\n- project-name/public/admin.php\n\nWe must register our module on a manual way, write this to the file located at ``project-name/app/modules.php``.\n\n``` php \nreturn [\n    # some array of modules above\n\n    'admin' => function (Phalcon\\Di\\FactoryDefault $di) {\n        $di->get('dispatcher')->setDefaultNamespace('App\\Admin\\Controllers');\n    },\n];\n```\n\nWe called the service ``dispatcher``, we've changed the default namespace to understand the namespace of our controller, this also helps the modules' router to be configured automatically.\n\nSetup your **Apache2** or **NginX** to point at ``project-name/public/admin.php`` and we're done! That's too simple, isn't it? Let's try below section as an alternative way.\n\n\n<a name=\"alternative-service\"></a>\n# Alternative as a Service\n\n```php\n<?php\nnamespace Popeye\\Admin;\n\nuse Phalcon\\Di\\FactoryDefault;\nuse Clarity\\Providers\\ServiceProvider;\n\nclass AdminServiceProvider extends ServiceProvider\n{\n    protected $alias = 'admin';\n    protected $shared = false;\n\n    private function getClosure()\n    {\n        return function (FactoryDefault $di) {\n            $di->get('dispatcher')\n               ->setDefaultNamespace('Popeye\\Admin\\App\\Controllers');\n        };\n    }\n\n    public function register()\n    {\n        di()->get('module')->setModule(\n            $this->alias,\n            $this->getClosure()\n        );\n\n        return $this;\n    }\n}\n```\n\n\nThe above code shows you how to include your module by calling ``di()->get('module')->setModule(<name>, <closure>)``\n\nLet's register this class as our service, go to ``project-name/config/app.php`` and find ``services``, include your class.\n\n```php\nreturn [\n\n    # some code above\n\n    'services' => [\n        # some service classes above\n\n        Popeye\\Admin\\AdminServiceProvider::class,\n    ],\n    \n    # some code below\n];\n```\n\ntry to access your page and it should be working fine.\n\n# Sample Service Modules\n\n- <a target=\"_blank\" href=\"https://github.com/daison12006013/phalconslayer-admin\">https://github.com/daison12006013/phalconslayer-admin</a>","updates":[],"order":0,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"56d6449db939870b00512c63","__v":52,"githubsync":"","createdAt":"2016-03-02T01:40:45.582Z","user":"56c1105874f0b417004baadc","category":{"sync":{"isSync":false,"url":""},"pages":["56d6449db939870b00512c63","56d648d270eba32d006060ae","56d648da70eba32d006060b0","56d648e870eba32d006060b2","56e23cde7d54ba0e00463568"],"title":"Modular-MVC","slug":"modular-mvc","order":2,"from_sync":false,"reference":false,"_id":"56c4275048213b1700af6e33","createdAt":"2016-02-17T07:54:56.500Z","project":"56c111095abfe40d00be875a","version":"56c111095abfe40d00be875d","__v":5},"version":{"version":"1.3.0","version_clean":"1.3.0","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["56c1110a5abfe40d00be875e","56c413a254b6030d00ec299d","56c4275048213b1700af6e33","56c42826c0c4630d004e86cb","56c4282cbc41330d009f2607","56c4284ad1f6d91700d3697e","56e271c195d1c60e00a969ee"],"_id":"56c111095abfe40d00be875d","releaseDate":"2016-02-14T23:43:05.566Z","__v":7,"createdAt":"2016-02-14T23:43:05.566Z","project":"56c111095abfe40d00be875a"},"parentDoc":null,"project":"56c111095abfe40d00be875a"}
# Module **<a href="http://google.com/search?q=what+is+module" target="_blank">What is module all about?</a>** In real world scenarios, we're building applications that communicates or uses the same resources, most probably you're building a large web application that must be divided into a different module, such as ``admin``, ``white label``, ``social network manager``, ``online payment manager`` and anything you think you must separate it from your current module. ## Index: - [Basic Usage](#basic-usage) - [Alternative as a Service](#alternative-service) <a name="basic-usage"></a> # Basic Usage Let us say, we want an ``admin`` module. Run this to your console: ``` php brood app:module admin ``` There must be generated directory and files, base it to these locations: - project-name/app/admin/ - project-name/public/admin.php We must register our module on a manual way, write this to the file located at ``project-name/app/modules.php``. ``` php return [ # some array of modules above 'admin' => function (Phalcon\Di\FactoryDefault $di) { $di->get('dispatcher')->setDefaultNamespace('App\Admin\Controllers'); }, ]; ``` We called the service ``dispatcher``, we've changed the default namespace to understand the namespace of our controller, this also helps the modules' router to be configured automatically. Setup your **Apache2** or **NginX** to point at ``project-name/public/admin.php`` and we're done! That's too simple, isn't it? Let's try below section as an alternative way. <a name="alternative-service"></a> # Alternative as a Service ```php <?php namespace Popeye\Admin; use Phalcon\Di\FactoryDefault; use Clarity\Providers\ServiceProvider; class AdminServiceProvider extends ServiceProvider { protected $alias = 'admin'; protected $shared = false; private function getClosure() { return function (FactoryDefault $di) { $di->get('dispatcher') ->setDefaultNamespace('Popeye\Admin\App\Controllers'); }; } public function register() { di()->get('module')->setModule( $this->alias, $this->getClosure() ); return $this; } } ``` The above code shows you how to include your module by calling ``di()->get('module')->setModule(<name>, <closure>)`` Let's register this class as our service, go to ``project-name/config/app.php`` and find ``services``, include your class. ```php return [ # some code above 'services' => [ # some service classes above Popeye\Admin\AdminServiceProvider::class, ], # some code below ]; ``` try to access your page and it should be working fine. # Sample Service Modules - <a target="_blank" href="https://github.com/daison12006013/phalconslayer-admin">https://github.com/daison12006013/phalconslayer-admin</a>