Skip to content

Commit 91d714a

Browse files
Fixed: Route middlewares, view route name, view css & js, Added: ghost components, added css and js func, Remaster Ghost, clean some code, route need to be clean someday...
1 parent dd89160 commit 91d714a

12 files changed

Lines changed: 271 additions & 37 deletions

File tree

App/Controllers/WelcomeController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class WelcomeController extends Controller
1919
{
20-
private $model_name = false;
20+
private $model_name = false;
2121
private $table = false;
2222

2323
public function welcome()
@@ -27,6 +27,6 @@ public function welcome()
2727

2828
public function demo()
2929
{
30-
return View::make('welcome');
30+
return View::make('welcome')->withComponent($data);;
3131
}
3232
}

App/Middlewares/Auth.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,30 @@
2020
use Silver\Http\Response;
2121
use Closure;
2222
use Silver\Http\Session;
23+
use Silver\Http\Redirect;
2324

2425
class Auth implements MiddlewareInterface
2526
{
2627
// put the name to make it public
2728
private $unguard = [
2829
'unguard',
29-
'home',
30+
'guest',
31+
'public',
3032
];
3133

3234
public function execute(Request $req, Response $res, Closure $next)
3335
{
3436
if($req->route() == null)
3537
return $next();
3638

37-
if (array_search($req->route()->name(), $this->unguard) !== false) {
39+
if (! array_search($req->route()->middleware(), $this->unguard) !== false) {
40+
//Change here if you want to check if someone is loggin in owherwise use Error 404;
3841

39-
return $next();
40-
}
41-
else{
42-
//put your secure code here!
43-
return $next();
42+
//Redirect::to('/');
43+
return \Silver\Http\View::error('404');
4444
}
45-
}
4645

47-
private function getHeaders()
48-
{
49-
return $this->headers;
46+
return $next();
5047
}
5148

5249
}
53-
54-

App/Routes.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,4 @@
1414

1515
use Silver\Core\Route;
1616

17-
Route::get('/', 'Welcome@welcome', 'home');
18-
19-
Route::get('/demo', 'Welcome@demo', 'unguard');
17+
Route::get('/', 'Welcome@welcome', 'home', 'public');
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<div class="">
2+
<textarea name="name" rows="8" cols="80" placeholder="lalalalala"></textarea>
3+
<div class="comments">
4+
<div class="row">
5+
<div class="col-lg-8">
6+
7+
<!-- #foreach($component_payload as $data)
8+
<div class="col-md-12">
9+
<label for="">comments: </label><br>
10+
<p>{{ $data }}</p>
11+
<hr>
12+
</div>
13+
#endforeach -->
14+
15+
</div>
16+
</div>
17+
</div>
18+
</div>

App/Views/errors/404.ghost.tpl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>404</title>
5+
<link rel="icon" href="<?=URL;?>system/libs/img/ico.png" type="image/png">
6+
<style type="text/css">
7+
body {
8+
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABZ0RVh0Q3JlYXRpb24gVGltZQAxMC8yOS8xMiKqq3kAAAAcdEVYdFNvZnR3YXJlAEFkb2JlIEZpcmV3b3JrcyBDUzVxteM2AAABHklEQVRIib2Vyw6EIAxFW5idr///Qx9sfG3pLEyJ3tAwi5EmBqRo7vHawiEEERHS6x7MTMxMVv6+z3tPMUYSkfTM/R0fEaG2bbMv+Gc4nZzn+dN4HAcREa3r+hi3bcuu68jLskhVIlW073tWaYlQ9+F9IpqmSfq+fwskhdO/AwmUTJXrOuaRQNeRkOd5lq7rXmS5InmERKoER/QMvUAPlZDHcZRhGN4CSeGY+aHMqgcks5RrHv/eeh455x5KrMq2yHQdibDO6ncG/KZWL7M8xDyS1/MIO0NJqdULLS81X6/X6aR0nqBSJcPeZnlZrzN477NKURn2Nus8sjzmEII0TfMiyxUuxphVWjpJkbx0btUnshRihVv70Bv8ItXq6Asoi/ZiCbU6YgAAAABJRU5ErkJggg==);
9+
color: #484848;
10+
-webkit-font-smoothing:antialiased;
11+
font-weight: lighter;
12+
text-rendering: optimizeLegibility;
13+
font-size: 18px;
14+
font-family: 'Roboto', sans-serif;
15+
16+
}
17+
.error-template {
18+
padding: 40px 15px;text-align: center;
19+
}
20+
.error-actions {
21+
margin-top:15px;margin-bottom:15px;
22+
}
23+
.error-actions .btn {
24+
margin-right:10px;
25+
}
26+
</style>
27+
</head>
28+
<body>
29+
<div class="container">
30+
<div class="row">
31+
<div class="col-md-12">
32+
<div class="error-template">
33+
<h1>
34+
Oops!</h1>
35+
<h2>
36+
404 Page Not Found</h2>
37+
<div class="error-details">
38+
Sorry, an error has occured, Requested page not found!
39+
</div>
40+
</div>
41+
</div>
42+
</div>
43+
</div>
44+
</body>
45+
</html>

App/Views/layouts/master.ghost.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
<meta name="description" content="">
88
<meta name="author" content="">
99
<title>SilverEngine</title>
10+
11+
{{ css('style') }}
12+
1013
</head>
1114
1215
<body>
1316
1417
#block(content)
1518
19+
<a href="{{ route('home') }}">welcome</a>
20+
21+
{{ js('app') }}
22+
1623
</body>
17-
</html>
24+
</html>

App/Views/welcome.ghost.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{{ extends('layouts.master') }}
22

33
#set[content]
4+
45
Welcome to SilverEngine demo page
5-
#end
6+
7+
#end

System/Core/Library.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ function js($url, $attrs = [])
130130
if (!function_exists('route')) {
131131
function route($name, $args = [])
132132
{
133-
return Route::getRoute($name)->url($args);
133+
//TODO: catch error if route not exists,
134+
return Route::getRoute($name)->url($args);
134135
}
135136
}
136137

@@ -146,4 +147,4 @@ function mapArrayKeys($array,callable $callback)
146147
}
147148
return $arr;
148149
}
149-
}
150+
}

System/Core/Route.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Route
2121
private $_action;
2222
private $_jails;
2323
private $_name;
24+
private $_middleware;
2425
private $_type;
2526
private $_variables = [];
2627

@@ -43,13 +44,14 @@ class Route
4344
* @param null $name
4445
* @param null $type
4546
*/
46-
public function __construct($method, $route, $action, $name = null, $type = null)
47+
public function __construct($method, $route, $action, $name = null, $middleware = 'public', $type = null)
4748
{
4849
$this->_method = strtolower($method);
4950
$this->_route = $route;
5051
$this->_action = $action;
5152
$this->_jails = self::$jails;
5253
$this->_name = $name;
54+
$this->_middleware = $middleware;
5355
$this->_type = $type;
5456
}
5557

@@ -77,6 +79,14 @@ public function name()
7779
return $this->_name;
7880
}
7981

82+
/**
83+
* @return null
84+
*/
85+
public function middleware()
86+
{
87+
return $this->_middleware;
88+
}
89+
8090
/**
8191
* @return string
8292
*/
@@ -223,6 +233,7 @@ public function check($method, $url)
223233

224234

225235
// Check jails
236+
//TODO: deprecated!
226237
foreach ($this->_jails as $jail) {
227238
$jail = explode('@', $jail);
228239

@@ -246,6 +257,7 @@ public function check($method, $url)
246257
public static function group($args, $fn)
247258
{
248259
// Prepare
260+
//TODO: deprecated use MW
249261
if (isset($args['jail'])) {
250262
self::$jails[] = $args['jail'];
251263
}
@@ -262,6 +274,7 @@ public static function group($args, $fn)
262274
// Restore
263275
self::$_prefix = $old_prefix;
264276

277+
// TODO: deprecated use MW
265278
if (isset($args['jail'])) {
266279
array_pop(self::$jails);
267280
}
@@ -289,11 +302,11 @@ public static function find($requestUrl, $requestMethod)
289302
* @param null $name
290303
* @param string $type
291304
*/
292-
public static function register($method, $route, $action, $name = null, $type = '')
305+
public static function register($method, $route, $action, $name = null, $middleware = 'public', $type = '')
293306
{
294307
$route = self::$_prefix . $route;
295308
foreach (explode("|", $method) as $m) {
296-
$r = new Route($m, $route, $action, $name, $type);
309+
$r = new Route($m, $route, $action, $name, $middleware, $type);
297310
self::$_routes[] = $r;
298311
self::$_route_index[ $name ] = $r;
299312
}
@@ -304,19 +317,19 @@ public static function register($method, $route, $action, $name = null, $type =
304317
* @param $action
305318
* @param null $name
306319
*/
307-
public static function get($route, $action, $name = null)
320+
public static function get($route, $action, $name = null, $middleware ='public')
308321
{
309-
return self::register("get", $route, $action, $name, 'get');
322+
return self::register("get", $route, $action, $name, $middleware, 'get');
310323
}
311324

312325
/**
313326
* @param $route
314327
* @param $action
315328
* @param null $name
316329
*/
317-
public static function post($route, $action, $name = null)
330+
public static function post($route, $action, $name = null, $middleware ='public')
318331
{
319-
return self::register("post", $route, $action, $name, 'post');
332+
return self::register("post", $route, $action, $name,$middleware, 'post');
320333
}
321334

322335

@@ -327,9 +340,9 @@ public static function post($route, $action, $name = null)
327340
* @param null $name
328341
* @param string $req
329342
*/
330-
public static function put($route, $action, $name = null, $req = 'put')
343+
public static function put($route, $action, $name = null, $middleware ='public', $req = 'put')
331344
{
332-
return self::register("put", $route, $action, $name, $req);
345+
return self::register("put", $route, $action, $name,$middleware, $req);
333346
}
334347

335348
// need to work on POST too
@@ -339,9 +352,9 @@ public static function put($route, $action, $name = null, $req = 'put')
339352
* @param null $name
340353
* @param string $req
341354
*/
342-
public static function delete($route, $action, $name = null, $req = 'delete')
355+
public static function delete($route, $action, $name = null, $middleware ='public', $req = 'delete')
343356
{
344-
return self::register("delete", $route, $action, $name, $req);
357+
return self::register("delete", $route, $action, $name, $middleware, $req);
345358
}
346359

347360

@@ -350,7 +363,7 @@ public static function delete($route, $action, $name = null, $req = 'delete')
350363
* @param $action
351364
* @param null $name
352365
*/
353-
public static function resource($route, $action, $name = null)
366+
public static function resource($route, $action, $name = null, $middleware ='public')
354367
{
355368
// Remove possible method name
356369
if (($pos = strpos($action, '@')) !== false) {
@@ -391,9 +404,9 @@ public static function resource($route, $action, $name = null)
391404
* @param $action
392405
* @param null $name
393406
*/
394-
public static function any($route, $action, $name = null)
407+
public static function any($route, $action, $name = null, $middleware ='public')
395408
{
396-
return self::register("any", $route, $action, $name, 'any');
409+
return self::register("any", $route, $action, $name, $middleware, 'any');
397410
}
398411

399412
/**

0 commit comments

Comments
 (0)