Skip to content

Commit 2cba0ed

Browse files
EmilioEmilio
Emilio
authored and
Emilio
committed
added structure to Backend, Frontend and Core
1 parent 73d141b commit 2cba0ed

File tree

25 files changed

+701
-0
lines changed

25 files changed

+701
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea/
2+
.phalcon/
3+
cache/
4+
vendor/
5+
composer.lock

app/Bootstrap.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Phalcon\Config;
6+
use Phalcon\Debug;
7+
use Phalcon\DI\FactoryDefault;
8+
use Phalcon\Mvc\Application;
9+
use Phalcon\Mvc\Model;
10+
11+
/**
12+
* Class Bootstrap
13+
*/
14+
class Bootstrap extends Application
15+
{
16+
/**
17+
* Bootstrap constructor.
18+
*/
19+
public function __construct()
20+
{
21+
// init DI and config service
22+
$this->setDI(new FactoryDefault());
23+
24+
// general configuration
25+
$config = new Config(require_once APP_PATH . '/config/config.php');
26+
$this->getDI()->setShared('config', $config);
27+
28+
// ORM setup
29+
Model::setup($config->orm->toArray());
30+
31+
// register services
32+
$this->registerServices();
33+
34+
// register modules
35+
$this->registerModules($config->application->modules->toArray());
36+
$this->setDefaultModule($config->application->moduleDefault);
37+
}
38+
39+
/**
40+
* Services registry
41+
*/
42+
private function registerServices(): void
43+
{
44+
$di = $this->getDI();
45+
$config = $this->getDI()->getShared('config');
46+
47+
// custom config by environment?
48+
if (is_file(APP_PATH . '/config/config.' . APP_ENV . '.php')) {
49+
$config->merge(new Config(require_once APP_PATH . '/config/config.' . APP_ENV . '.php'));
50+
}
51+
52+
// debug mode?
53+
if ($config->get('debug', false) === true) {
54+
error_reporting(E_ALL);
55+
ini_set('display_errors', 1);
56+
(new Debug())->listen(true, true);
57+
}
58+
59+
require_once APP_PATH . '/config/loader.php';
60+
require_once APP_PATH . '/config/services.php';
61+
}
62+
63+
/**
64+
* Init
65+
* @return string
66+
*/
67+
public function init(): string
68+
{
69+
mb_internal_encoding('UTF-8');
70+
mb_http_output('UTF-8');
71+
72+
return $this->handle()->getContent();
73+
}
74+
}

app/config/config.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
return [
4+
'debug' => true,
5+
'database' => [
6+
'adapter' => 'Mysql',
7+
'host' => 'localhost',
8+
'username' => 'root',
9+
'password' => '',
10+
'dbname' => 'test',
11+
'charset' => 'utf8',
12+
],
13+
'application' => [
14+
'appDir' => APP_PATH . '/',
15+
'libraryDir' => APP_PATH . '/library/',
16+
'cacheDir' => APP_PATH . '/../cache/',
17+
'modules' => [
18+
'Frontend' => [
19+
'className' => Frontend\Module::class,
20+
'path' => APP_PATH . '/modules/Frontend/Module.php',
21+
],
22+
'Backend' => [
23+
'className' => Backend\Module::class,
24+
'path' => APP_PATH . '/modules/Backend/Module.php',
25+
],
26+
],
27+
'moduleDefault' => 'Frontend',
28+
'namespaces' => [
29+
'Core' => APP_PATH . '/modules/Core/',
30+
'Frontend' => APP_PATH . '/modules/Frontend/',
31+
'Backend' => APP_PATH . '/modules/Backend/',
32+
],
33+
],
34+
'orm' => [
35+
'castOnHydrate' => true,
36+
'notNullValidations' => false,
37+
]
38+
];

app/config/loader.php

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
/** @var Phalcon\Config $config */
3+
$loader = new \Phalcon\Loader();
4+
$loader->registerNamespaces($config->application->namespaces->toArray());
5+
$loader->register();

app/config/services.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* Shared configuration service
5+
*/
6+
/** @var Phalcon\Di $di */
7+
$di->setShared('router', function () {
8+
$config = $this->getShared('config');
9+
10+
$router = new \Phalcon\Mvc\Router(false);
11+
$router->removeExtraSlashes(true)
12+
->setDefaultModule($config->application->moduleDefault);
13+
14+
// mount module routes
15+
foreach (array_keys($config->application->modules->toArray()) as $module) {
16+
if (!is_file(APP_PATH . '/modules/' . $module . '/config/routers.php')) {
17+
continue;
18+
}
19+
20+
$routers = require_once APP_PATH . '/modules/' . $module . '/config/routers.php';
21+
22+
if (!empty($routers) && is_array($routers)) {
23+
foreach ($routers as $route) {
24+
$router->mount(new $route());
25+
}
26+
}
27+
}
28+
29+
return $router;
30+
});
31+
32+
/**
33+
* The URL component is used to generate all kind of urls in the application
34+
*/
35+
$di->setShared('url', function () {
36+
$config = $this->getShared('config');
37+
$url = new \Phalcon\Mvc\Url();
38+
39+
if (isset($config->application->baseUri)) {
40+
$url->setBaseUri($config->application->baseUri);
41+
}
42+
43+
return $url;
44+
});
45+
46+
$di->setShared('voltService', function ($view) {
47+
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $this);
48+
$directory = $this->getShared('config')->application->cacheDir . '/volt';
49+
50+
if (!is_dir($directory)) {
51+
mkdir($directory, 0755, true);
52+
}
53+
54+
$volt->setOptions([
55+
"compiledPath" => $directory,
56+
"compiledSeparator" => "-",
57+
"compileAlways" => true,
58+
]);
59+
60+
return $volt;
61+
});
62+
63+
/**
64+
* Database connection is created based in the parameters defined in the configuration file
65+
*/
66+
$di->setShared('db', function () {
67+
$config = $this->getShared('config');
68+
69+
$class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
70+
$params = [
71+
'host' => $config->database->host,
72+
'username' => $config->database->username,
73+
'password' => $config->database->password,
74+
'dbname' => $config->database->dbname,
75+
'charset' => $config->database->charset,
76+
];
77+
78+
if ($config->database->adapter == 'Postgresql') {
79+
unset($params['charset']);
80+
}
81+
82+
$connection = new $class($params);
83+
84+
return $connection;
85+
});
86+
87+
88+
/**
89+
* If the configuration specify the use of metadata adapter use it or use memory otherwise
90+
*/
91+
$di->setShared('modelsMetadata', function () {
92+
return new \Phalcon\Mvc\Model\Metadata\Memory();
93+
});
94+
95+
/**
96+
* Register the session flash service with the Twitter Bootstrap classes
97+
*/
98+
$di->setShared('flash', function () {
99+
return new \Phalcon\Flash\Direct([
100+
'error' => 'alert alert-danger',
101+
'success' => 'alert alert-success',
102+
'notice' => 'alert alert-info',
103+
'warning' => 'alert alert-warning',
104+
]);
105+
});
106+
107+
/**
108+
* Start the session the first time some component request the session service
109+
*/
110+
$di->setShared('session', function () {
111+
$session = new \Phalcon\Session\Adapter\Files();
112+
$session->start();
113+
114+
return $session;
115+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Backend\Controllers;
4+
5+
use Phalcon\Mvc\Controller;
6+
7+
abstract class ControllerBase extends Controller
8+
{
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Backend\Controllers;
4+
5+
/**
6+
* Class IndexController
7+
*
8+
* @package Backend\Controllers
9+
*/
10+
class IndexController extends ControllerBase
11+
{
12+
/**
13+
* Index action
14+
*/
15+
public function indexAction(): void
16+
{
17+
}
18+
}

app/modules/Backend/Module.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Backend;
4+
5+
use Phalcon\Config;
6+
use Phalcon\DiInterface;
7+
use Phalcon\Mvc\ModuleDefinitionInterface;
8+
9+
/**
10+
* Class Module
11+
* Setup backend module
12+
*
13+
* @package Backend
14+
*/
15+
class Module implements ModuleDefinitionInterface
16+
{
17+
/**
18+
* Module path
19+
*/
20+
const PATH = __DIR__;
21+
22+
/**
23+
* Registers the module auto-loader
24+
*
25+
* @param DiInterface|null $di
26+
*/
27+
public function registerAutoloaders(DiInterface $di = null): void
28+
{
29+
}
30+
31+
/**
32+
* Registers the module-only services
33+
*
34+
* @param DiInterface $di
35+
*/
36+
public function registerServices(DiInterface $di): void
37+
{
38+
$di->get('config')->merge(new Config(require_once self::PATH . "/config/config.php"));
39+
40+
require_once self::PATH . "/config/services.php";
41+
}
42+
}

app/modules/Backend/Routers/Main.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Backend\Routers;
4+
5+
use Phalcon\Mvc\Router\Group;
6+
7+
/**
8+
* Class Main
9+
* Basic router
10+
*
11+
* @package Backend\Routers
12+
*/
13+
class Main extends Group
14+
{
15+
/**
16+
* Admin home page
17+
*/
18+
const ADMIN = 'backendMainHome';
19+
20+
/**
21+
* Routes with only controllers
22+
*/
23+
const CONTROLLER = 'backendMainController';
24+
25+
/**
26+
* Routes with action and/or params
27+
*/
28+
const ACTION = 'backendMainAction';
29+
30+
/**
31+
* Main router definitions
32+
*/
33+
public function initialize(): void
34+
{
35+
$this->setPaths([
36+
'module' => 'Backend',
37+
]);
38+
39+
$this->setPrefix('/admin');
40+
41+
$this->add('')->setName(self::ADMIN);
42+
43+
$this->add(
44+
'/:controller',
45+
['controller' => 1]
46+
)->setName(self::CONTROLLER);
47+
48+
$this->add(
49+
'/:controller/:action/:params',
50+
[
51+
'controller' => 1,
52+
'action' => 2,
53+
'params' => 3,
54+
]
55+
)->setName(self::ACTION);
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Backend index</h1>

app/modules/Backend/config/config.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'application' => [
5+
'controllersDir' => \Backend\Module::PATH . '/Controllers/',
6+
'viewsDir' => \Backend\Module::PATH . '/Views/Default/scripts/',
7+
]
8+
];
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
/**
4+
* Array of routes
5+
*/
6+
return [
7+
\Backend\Routers\Main::class,
8+
];

0 commit comments

Comments
 (0)