From f136a08919c18558add89d87422c7ac0bfcee3bf Mon Sep 17 00:00:00 2001 From: Ark4ne Date: Wed, 20 Feb 2019 00:06:49 +0100 Subject: [PATCH] cleanup base app --- .const.ini | 2 +- app/Core/Models/Viewable.php | 35 ------------------ app/Kernels/Cli/Kernel.php | 6 +-- .../Http/Controllers/ErrorsController.php | 8 ++-- .../Frontend/Controllers/AuthController.php | 8 ---- .../Micro/Controllers/MicroController.php | 3 +- resources/views/layouts/template.volt | 2 +- routes/http.php | 16 +++++++- routes/micro.php | 37 +++++++++++-------- 9 files changed, 45 insertions(+), 72 deletions(-) delete mode 100644 app/Core/Models/Viewable.php diff --git a/.const.ini b/.const.ini index b4ec9ab..4414d41 100644 --- a/.const.ini +++ b/.const.ini @@ -21,4 +21,4 @@ driver = file [session] id = nucleon-session-id -driver = files \ No newline at end of file +driver = files diff --git a/app/Core/Models/Viewable.php b/app/Core/Models/Viewable.php deleted file mode 100644 index 5e37b41..0000000 --- a/app/Core/Models/Viewable.php +++ /dev/null @@ -1,35 +0,0 @@ -$name; - } - $func = Str::studly($name); - if (method_exists($this, 'get' . $func)) { - return $this->{'get' . $func}; - } - - if (method_exists($this, 'is' . $func)) { - return $this->{'is' . $func}; - } - - return null; - } -} diff --git a/app/Kernels/Cli/Kernel.php b/app/Kernels/Cli/Kernel.php index 6f66589..8a58493 100644 --- a/app/Kernels/Cli/Kernel.php +++ b/app/Kernels/Cli/Kernel.php @@ -52,11 +52,9 @@ class Kernel extends CliKernel ]; /** - * Return the Middleware List to load. + * List of Global Middleware to attach onto the application. * * @var string[] */ - protected $middlewares = [ - // DebugMiddleware::class - ]; + protected $middlewares = []; } diff --git a/app/Kernels/Http/Controllers/ErrorsController.php b/app/Kernels/Http/Controllers/ErrorsController.php index f208e22..1a8ea6f 100644 --- a/app/Kernels/Http/Controllers/ErrorsController.php +++ b/app/Kernels/Http/Controllers/ErrorsController.php @@ -11,14 +11,14 @@ class ErrorsController extends ControllerBase { public function indexAction() { - $this->response->setStatusCode(500); + $this->response->setStatusCode(500, 'Internal Server Error'); return $this->view->render('errors', 'http5xx'); } public function http404Action() { - $this->response->setStatusCode(404); + $this->response->setStatusCode(404, 'Not Found'); return $this->view->render('errors', 'http404'); } @@ -32,9 +32,9 @@ public function throwExceptionAction() trigger_error('warning', E_USER_WARNING); try { - throw new \Exception('A catched exception'); + throw new \Phalcon\Exception('A catched exception', 159); } catch (\Exception $e) { - throw new \Phalcon\Exception('An uncaught exception', $e->getCode(), $e); + throw new \RuntimeException("that's a white rabbit", 0, $e); } } } diff --git a/app/Kernels/Http/Modules/Frontend/Controllers/AuthController.php b/app/Kernels/Http/Modules/Frontend/Controllers/AuthController.php index 3a1622e..6070a2e 100644 --- a/app/Kernels/Http/Modules/Frontend/Controllers/AuthController.php +++ b/app/Kernels/Http/Modules/Frontend/Controllers/AuthController.php @@ -13,14 +13,6 @@ class AuthController extends ControllerBase { - protected function onConstruct() - { - parent::onConstruct(); - - $this->middleware(RedirectIfAuthenticated::class)->except(['logout']); - $this->middleware(Csrf::class)->only(['postRegister', 'postLogin']); - } - /** * Register view. * diff --git a/app/Kernels/Micro/Controllers/MicroController.php b/app/Kernels/Micro/Controllers/MicroController.php index 4f16ed6..6f1e6d6 100644 --- a/app/Kernels/Micro/Controllers/MicroController.php +++ b/app/Kernels/Micro/Controllers/MicroController.php @@ -2,7 +2,6 @@ namespace App\Kernels\Micro\Controllers; -//use Neutrino\Http\Controller; use Phalcon\Mvc\Controller; class MicroController extends Controller @@ -15,4 +14,4 @@ public function indexAction() return $this->response; } -} \ No newline at end of file +} diff --git a/resources/views/layouts/template.volt b/resources/views/layouts/template.volt index 16aab36..a25ef47 100644 --- a/resources/views/layouts/template.volt +++ b/resources/views/layouts/template.volt @@ -11,7 +11,7 @@ {% do assets.outputCss('common.css') %} {# Block for specific css #} - {% do assets.addCss('/css/app.css') %} + {% do assets.addCss('css/app.css') %} {% block stylesheets %} {% endblock %} {# Output other css #} diff --git a/routes/http.php b/routes/http.php index 73e96a8..b12cb7d 100644 --- a/routes/http.php +++ b/routes/http.php @@ -60,21 +60,35 @@ $frontend->addGet('/register', [ 'controller' => 'auth', 'action' => 'register', + 'middleware' => [ + \App\Kernels\Http\Middleware\RedirectIfAuthenticated::class + ] ]); $frontend->addPost('/register', [ 'controller' => 'auth', 'action' => 'postRegister', + 'middleware' => [ + \App\Kernels\Http\Middleware\RedirectIfAuthenticated::class, + \Neutrino\Http\Middleware\Csrf::class + ] ]); $frontend->addGet('/login', [ 'controller' => 'auth', 'action' => 'login', + 'middleware' => [ + \App\Kernels\Http\Middleware\RedirectIfAuthenticated::class + ] ]); $frontend->addPost('/login', [ 'controller' => 'auth', 'action' => 'postLogin', + 'middleware' => [ + \App\Kernels\Http\Middleware\RedirectIfAuthenticated::class, + \Neutrino\Http\Middleware\Csrf::class + ] ]); $frontend->addGet('/logout', [ @@ -96,4 +110,4 @@ $backend->addGet('/back/:controller/:action'); -$router->mount($backend); \ No newline at end of file +$router->mount($backend); diff --git a/routes/micro.php b/routes/micro.php index cbb04ce..4d9fe52 100644 --- a/routes/micro.php +++ b/routes/micro.php @@ -39,20 +39,21 @@ | Api - Routes |-------------------------------------------------------------------------- */ +$router->addGet('/api/index', [ + 'controller' => \App\Kernels\Micro\Controllers\MicroController::class, + 'action' => 'indexAction' +]); + $router->addGet('/api/test', function () { /** @var \App\Kernels\Micro\Kernel $this */ - $this->response->setStatusCode(200); - - $this->response->setJsonContent(['status' => 'found', 'code' => 200]); - - return $this->response; + return $this->response + ->setStatusCode(200, 'OK') + ->setJsonContent([ + 'status' => 'found', + 'code' => 200 + ]); }); -$router->addGet('/api/index', [ - 'controller' => \App\Kernels\Micro\Controllers\MicroController::class, - 'action' => 'indexAction' -]); - /* |-------------------------------------------------------------------------- | Api - NotFound @@ -60,11 +61,12 @@ */ $router->notFound(function () { /** @var \App\Kernels\Micro\Kernel $this */ - $this->response->setStatusCode(404); - - $this->response->setJsonContent(['status' => 'not found', 'code' => 404]); - - return $this->response; + return $this->response + ->setStatusCode(404, 'Not Found') + ->setJsonContent([ + 'status' => 'not found', + 'code' => 404 + ]); }); /* @@ -81,6 +83,8 @@ return $this->response ->setStatusCode(401, 'Unauthorized') ->setJsonContent([ + 'status' => 'Unauthorized', + 'code' => 401, 'error' => 'Token mismatch', ]); } @@ -92,6 +96,7 @@ return $this->response ->setStatusCode(500, 'Internal Server Error') ->setJsonContent([ - 'error' => 'Internal Server Error', + 'status' => 'Internal Server Error', + 'code' => 500, ]); });