Skip to content

Commit 3309f5a

Browse files
committed
Initial commit
0 parents  commit 3309f5a

File tree

23 files changed

+417
-0
lines changed

23 files changed

+417
-0
lines changed

application/acl/account.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return [
4+
'all' => [
5+
//
6+
],
7+
'authorize' => [
8+
//
9+
],
10+
'guest' => [
11+
'register',
12+
'login',
13+
],
14+
'admin' => [
15+
//
16+
],
17+
];

application/acl/main.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
return [
4+
'all' => [
5+
'index',
6+
],
7+
'authorize' => [
8+
//
9+
],
10+
'guest' => [
11+
//
12+
],
13+
'admin' => [
14+
//
15+
],
16+
];

application/config/db.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'host' => '',
5+
'name' => '',
6+
'user' => '',
7+
'password' => '',
8+
];

application/config/routes.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
return [
4+
5+
'' => [
6+
'controller' => 'main',
7+
'action' => 'index',
8+
],
9+
10+
'account/login' => [
11+
'controller' => 'account',
12+
'action' => 'login',
13+
],
14+
15+
'account/register' => [
16+
'controller' => 'account',
17+
'action' => 'register',
18+
],
19+
20+
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace application\controllers;
4+
5+
use application\core\Controller;
6+
7+
class AccountController extends Controller {
8+
9+
public function loginAction() {
10+
$this->view->render('Вход');
11+
}
12+
13+
public function registerAction() {
14+
$this->view->render('Регистрация');
15+
}
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace application\controllers;
4+
5+
use application\core\Controller;
6+
7+
class MainController extends Controller {
8+
9+
public function indexAction() {
10+
$result = $this->model->getNews();
11+
$vars = [
12+
'news' => $result,
13+
];
14+
$this->view->render('Главная страница', $vars);
15+
}
16+
17+
}

application/core/Controller.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace application\core;
4+
5+
use application\core\View;
6+
7+
abstract class Controller {
8+
9+
public $route;
10+
public $view;
11+
public $acl;
12+
13+
public function __construct($route) {
14+
$this->route = $route;
15+
if (!$this->checkAcl()) {
16+
View::errorCode(403);
17+
}
18+
$this->view = new View($route);
19+
$this->model = $this->loadModel($route['controller']);
20+
}
21+
22+
public function loadModel($name) {
23+
$path = 'application\models\\'.ucfirst($name);
24+
if (class_exists($path)) {
25+
return new $path;
26+
}
27+
}
28+
29+
public function checkAcl() {
30+
$this->acl = require 'application/acl/'.$this->route['controller'].'.php';
31+
if ($this->isAcl('all')) {
32+
return true;
33+
}
34+
elseif (isset($_SESSION['authorize']['id']) and $this->isAcl('authorize')) {
35+
return true;
36+
}
37+
elseif (!isset($_SESSION['authorize']['id']) and $this->isAcl('guest')) {
38+
return true;
39+
}
40+
elseif (isset($_SESSION['admin']) and $this->isAcl('admin')) {
41+
return true;
42+
}
43+
return false;
44+
}
45+
46+
public function isAcl($key) {
47+
return in_array($this->route['action'], $this->acl[$key]);
48+
}
49+
50+
}

application/core/Model.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace application\core;
4+
5+
use application\lib\Db;
6+
7+
abstract class Model {
8+
9+
public $db;
10+
11+
public function __construct() {
12+
$this->db = new Db;
13+
}
14+
15+
}

application/core/Router.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace application\core;
4+
5+
use application\core\View;
6+
7+
class Router {
8+
9+
protected $routes = [];
10+
protected $params = [];
11+
12+
public function __construct() {
13+
$arr = require 'application/config/routes.php';
14+
foreach ($arr as $key => $val) {
15+
$this->add($key, $val);
16+
}
17+
}
18+
19+
public function add($route, $params) {
20+
$route = '#^'.$route.'$#';
21+
$this->routes[$route] = $params;
22+
}
23+
24+
public function match() {
25+
$url = trim($_SERVER['REQUEST_URI'], '/');
26+
foreach ($this->routes as $route => $params) {
27+
if (preg_match($route, $url, $matches)) {
28+
$this->params = $params;
29+
return true;
30+
}
31+
}
32+
return false;
33+
}
34+
35+
public function run(){
36+
if ($this->match()) {
37+
$path = 'application\controllers\\'.ucfirst($this->params['controller']).'Controller';
38+
if (class_exists($path)) {
39+
$action = $this->params['action'].'Action';
40+
if (method_exists($path, $action)) {
41+
$controller = new $path($this->params);
42+
$controller->$action();
43+
} else {
44+
View::errorCode(404);
45+
}
46+
} else {
47+
View::errorCode(404);
48+
}
49+
} else {
50+
View::errorCode(404);
51+
}
52+
}
53+
54+
}

application/core/View.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace application\core;
4+
5+
class View {
6+
7+
public $path;
8+
public $route;
9+
public $layout = 'default';
10+
11+
public function __construct($route) {
12+
$this->route = $route;
13+
$this->path = $route['controller'].'/'.$route['action'];
14+
}
15+
16+
public function render($title, $vars = []) {
17+
extract($vars);
18+
$path = 'application/views/'.$this->path.'.php';
19+
if (file_exists($path)) {
20+
ob_start();
21+
require $path;
22+
$content = ob_get_clean();
23+
require 'application/views/layouts/'.$this->layout.'.php';
24+
}
25+
}
26+
27+
public function redirect($url) {
28+
header('location: '.$url);
29+
exit;
30+
}
31+
32+
public static function errorCode($code) {
33+
http_response_code($code);
34+
$path = 'application/views/errors/'.$code.'.php';
35+
if (file_exists($path)) {
36+
require $path;
37+
}
38+
exit;
39+
}
40+
41+
public function message($status, $message) {
42+
exit(json_encode(['status' => $status, 'message' => $message]));
43+
}
44+
45+
public function location($url) {
46+
exit(json_encode(['url' => $url]));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)