From d21715303bd901f3a21fdf4d2644f160bd146c74 Mon Sep 17 00:00:00 2001 From: Carlos Eduardo Date: Tue, 21 Jun 2016 18:22:52 -0300 Subject: [PATCH] =?UTF-8?q?Base=20=E2=80=93=201.0.0-alpha.5.1.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- app/appsconfig.php | 7 --- app/appsconfig.php.example | 37 ------------- app/example/config.php | 30 +++++++++-- .../controller/logincontroller.class.php | 17 ++++++ base/core/application.class.php | 4 ++ base/core/routes/route.class.php | 6 +-- base/core/routes/router.class.php | 52 +++++++++++++++---- base/core/routes/rules/allows.class.php | 2 +- 9 files changed, 93 insertions(+), 64 deletions(-) delete mode 100644 app/appsconfig.php delete mode 100644 app/appsconfig.php.example diff --git a/README.md b/README.md index 5ea7309..80ff458 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Base – 1.0.0-alpha.5.1 +# Base – 1.0.0-alpha.5.1.1 Base é uma "base" para construção de soluções em MVC com PHP. Construi o _framework_ para uso próprio, porém resolvi disponibilizar para a comunidade em geral. diff --git a/app/appsconfig.php b/app/appsconfig.php deleted file mode 100644 index 7df13c2..0000000 --- a/app/appsconfig.php +++ /dev/null @@ -1,7 +0,0 @@ - Array( - 'url' => 'http://localhost/Base/public_html/', - ) -)); \ No newline at end of file diff --git a/app/appsconfig.php.example b/app/appsconfig.php.example deleted file mode 100644 index 9dfabaa..0000000 --- a/app/appsconfig.php.example +++ /dev/null @@ -1,37 +0,0 @@ - Array( - 'url' => 'http://contoso.com/', // Application URL - 'database' => Array( - 'driver' => 'mysql', // SQL Driver (mysql only avaliable) - 'host' => 'localhost', // Database Host connection - 'port' => 3306, // Database Port connection - 'user' => 'root', // Database User - 'pass' => '', // Databse Password - 'schema' => 'test', // Database Name - 'charset' => 'utf8' // Charset (if omitted 'utf8' is setted by default) - ), - 'email' => Array( - 'host' => 'smtp.contoso.com', // SMTP Host - 'port' => 465, // PORT - 'auth' => TRUE, // Authenticate? - 'secure' => 'ssl', // Encriptation tls | ssl? - 'user' => 'no-reply@contoso.com', // Authentication User - 'pass' => 'P455W0RD', // Authentication Password - 'from' => Array( // Who is sending de e-mail? - 'email'=>'contact@contoso.com', - 'name'=>'Contoso Site' - ) - ), - , - 'authentication' => Array( - 'class' => '\\Authentication\\Authentication', // Your Authentication Verification Class - 'method' => 'validateAccess', // Your Authentication Verification Method - 'redirect' => 'error/_401', // Redirect Route on fail - 'notcheckon' => Array( // Not Check On [Class => Action] | * is a wildcard - 'error' => '*' // Will no check on any method from class Error - ) - ) - ) -)); \ No newline at end of file diff --git a/app/example/config.php b/app/example/config.php index 0bff180..67ca6b1 100644 --- a/app/example/config.php +++ b/app/example/config.php @@ -3,36 +3,57 @@ use \Core\Routes\Router; use \Core\Config; +// Configura a aplicação, setando o name e URL +// Outras configurações também são possívels, como conexão com banco, +// Configurações de e-mail e etc. + Config::Set([ 'url' => 'http://localhost/Base/public_html/', 'name' => 'example', ]); +// Seta a rota de erro 404 Router::notfound([ 'controller' => 'Error', 'action' => 'index' ]); + +// Setá a rota padrão, home/index Router::main([ 'controller' => 'Main', 'action' => 'index' ]); +// Apenas replicando a rota padrão para ser acessada através do endereço /index Router::route('/index', Router::main()); -Router::route('/entrar',[ +// Rota para o formulário de LOGIN, acessível apenas via GET +Router::get('/entrar', 'entrar', [ 'controller' => 'Login', 'action' => 'index' ]); -Router::route('/post/:id',[ +// Replicando a rota /entrar para /login. Permitira acesso pelo mesmo Method que a rota /entrar (GET) +Router::route('/login', 'login.form', Router::GetByName('entrar')->_clone()); + +// Rota para realizar o login, acessível apenas via POST +Router::post('/login', 'login', [ + 'controller' => 'Login', + 'action' => 'login' +]); + + +// Rota: GET /post/ +Router::get('/post/:id',[ 'controller' => 'Main', 'action' => 'teste' ])->params([ 'id' => '\d+' ]); -Router::route('/post/:slug',[ +// Rota: GET /post/ +Router::get('/post/:slug',[ 'controller' => 'Main', 'action' => 'action' ])->params([ @@ -40,7 +61,8 @@ ]); -Router::route('/post/:date/:slug', [ +// Rota: GET /post// +Router::get('/post/:date/:slug', [ 'controller' => 'Main', 'action' => 'pdate' ])->params([ diff --git a/app/example/controller/logincontroller.class.php b/app/example/controller/logincontroller.class.php index 1f0b6c6..d98e9e4 100644 --- a/app/example/controller/logincontroller.class.php +++ b/app/example/controller/logincontroller.class.php @@ -23,4 +23,21 @@ function index() { ) ); } + + function login() { + return $this->load->content( + '%header% %menu%'. + '
'. + ''. + '
%login%
'. + '
'. + '%footer%', + Array( + 'header' => $this->load->view('commons/head'), + 'menu' => $this->load->view('commons/menu'), + 'footer' =>$this->load->view('commons/footer'), + 'login' => var_export($this->request->post, TRUE) + ) + ); + } } \ No newline at end of file diff --git a/base/core/application.class.php b/base/core/application.class.php index 86eb3ee..8c48b6f 100644 --- a/base/core/application.class.php +++ b/base/core/application.class.php @@ -32,6 +32,10 @@ public static function RUN($application) { $route = $router->GetByRequest(); + if (!$route) { + $route = Router::notfound(); + } + if ($route) { $request->controller = $route->controller; $request->action = $route->action; diff --git a/base/core/routes/route.class.php b/base/core/routes/route.class.php index e39c19b..55080d7 100644 --- a/base/core/routes/route.class.php +++ b/base/core/routes/route.class.php @@ -145,16 +145,12 @@ public function settings($options) { $options['pattern'] = $options['host']; if (empty($options['name'])) - $options['name'] = $options['host']; + $options['name'] = str_replace('/', '.', trim($options['host'], '/')); foreach ($options as $p => $value) if (is_callable([$this, $p])) $this->$p($value); - - //$this->setHost(); - - return $this; } diff --git a/base/core/routes/router.class.php b/base/core/routes/router.class.php index ea15f7e..6dc6a0b 100644 --- a/base/core/routes/router.class.php +++ b/base/core/routes/router.class.php @@ -75,9 +75,9 @@ function __construct($routes = NULL) { public static function main($options = NULL) { if (is_null($options)) - return self::$routes['home']; + return self::GetByName('default.index'); else - return self::register(['/', array_merge(['name'=>'home'], $options)]); + return self::register(['/', array_merge(['name'=>'default.index'], $options)]); } public static function notfound($options = NULL) { @@ -86,7 +86,7 @@ public static function notfound($options = NULL) { public static function error($code, $options = NULL) { if (empty($options)) - return self::$routes["error.{$code}"]; + return self::GetByName("error.{$code}"); else return self::register(["/{$code}", array_merge(['name' => "error.{$code}"], $options)]); } @@ -131,9 +131,9 @@ public static function add(Route $route) { if (is_null($route)) return FALSE; - //self::$routes->offsetSet($route->name, $route); + self::$routes->offsetSet($route->name, $route); - self::$routes[$route->name] = $route; + //self::$routes[$route->name] = $route; self::$route = $route; @@ -170,12 +170,13 @@ public static function add(Route $route) { */ public static function route($host, $name = NULL, $options = NULL, $callback = NULL, $route = NULL) { - $options = self::parseRouteOptions($host, $name, $options, $callback, $route); $route = self::parseRouteRoute($host, $name, $options, $callback, $route); $callback = self::parseRouteCallback($host, $name, $options, $callback, $route); + $options = self::parseRouteOptions($host, $name, $options, $callback, $route); + if (is_callable($callback)) $options['handler'] = $callback; if (!empty($name) && is_string($name)) $options['name'] = $name; @@ -184,6 +185,7 @@ public static function route($host, $name = NULL, $options = NULL, $callback = N $route = New Route($host, $options); else { $options['host'] = $host; + $route = $route->_clone($options); } @@ -231,11 +233,43 @@ private static function parseRouteRoute($host, $name = NULL, $options = NULL, $c return $route; } + /** + * Mesmo que Router::route([...])->allows('GET') ou Router::route([...,] ['allows'=>'GET'] [,...]) + * @return Route + */ + public static function get($host, $name = NULL, $options = NULL, $callback = NULL, $route = NULL) { + return self::route($host, $name, $options, $callback, $route)->allows('GET'); + } + + /** + * Mesmo que Router::route([...])->allows('POST') ou Router::route([...,] ['allows'=>'POST'] [,...]) + * @return Route + */ + public static function post($host, $name = NULL, $options = NULL, $callback = NULL, $route = NULL) { + return self::route($host, $name, $options, $callback, $route)->allows('POST'); + } + + /** + * Mesmo que Router::route([...])->allows('PUT') ou Router::route([...,] ['allows'=>'PUT'] [,...]) + * @return Route + */ + public static function put($host, $name = NULL, $options = NULL, $callback = NULL, $route = NULL) { + return self::route($host, $name, $options, $callback, $route)->allows('PUT'); + } + + /** + * Mesmo que Router::route([...])->allows('DELETE') ou Router::route([...,] $options = ['allows'=>'DELETE'] [,...]) + * @return Route + */ + public static function delete($host, $name = NULL, $options = NULL, $callback = NULL, $route = NULL) { + return self::route($host, $name, $options, $callback, $route)->allows('DELETE'); + } + public static function GetByName($name) { - if (!isset(self::$routes[$name])) - return NULL; + if (self::$routes instanceof Routes && self::$routes->offsetExists($name)) + return self::$routes->offsetGet($name); - return self::$routes[$name]; + return NULL; } public function GetByRequest() { diff --git a/base/core/routes/rules/allows.class.php b/base/core/routes/rules/allows.class.php index a87e443..b3308ee 100644 --- a/base/core/routes/rules/allows.class.php +++ b/base/core/routes/rules/allows.class.php @@ -22,7 +22,7 @@ public function __invoke(Request $request, Route $route) { return TRUE; } - $request = $request->getMethod() ?: 'GET'; + $requestMethod = $request->getMethod() ?: 'GET'; return in_array($requestMethod, $route->allows); } } \ No newline at end of file