Skip to content

Commit

Permalink
Base – 1.0.0-alpha.5.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
KaduAmaral committed Jun 21, 2016
1 parent c296c41 commit d217153
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
7 changes: 0 additions & 7 deletions app/appsconfig.php

This file was deleted.

37 changes: 0 additions & 37 deletions app/appsconfig.php.example

This file was deleted.

30 changes: 26 additions & 4 deletions app/example/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,66 @@
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/<id>
Router::get('/post/:id',[
'controller' => 'Main',
'action' => 'teste'
])->params([
'id' => '\d+'
]);

Router::route('/post/:slug',[
// Rota: GET /post/<algum-endereco>
Router::get('/post/:slug',[
'controller' => 'Main',
'action' => 'action'
])->params([
'slug' => '[a-zA-Z0-9\-_]+'
]);


Router::route('/post/:date/:slug', [
// Rota: GET /post/<DD-MM-YYYY>/<algum-endereco>
Router::get('/post/:date/:slug', [
'controller' => 'Main',
'action' => 'pdate'
])->params([
Expand Down
17 changes: 17 additions & 0 deletions app/example/controller/logincontroller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,21 @@ function index() {
)
);
}

function login() {
return $this->load->content(
'%header% %menu%'.
'<div class="container"><div class="col-xs-12 col-sm-6 col-sm-offset-3">'.
'<div class="page-header"><h1>Dados do Login</h1></div>'.
'<pre class="well">%login%</pre>'.
'</div></div>'.
'%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)
)
);
}
}
4 changes: 4 additions & 0 deletions base/core/application.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions base/core/routes/route.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
52 changes: 43 additions & 9 deletions base/core/routes/router.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)]);
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion base/core/routes/rules/allows.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit d217153

Please sign in to comment.