| php level_up |
| 1 | <?php namespace appcontrollers; use ishopCache; class MainController extends AppController { public function indexAction(){ $posts = R::findAll('test'); $post = R::findOne('test', 'id = ?', [2]); $this->setMeta('Главная страница', 'Описание...', 'Ключевики...'); $name = 'John'; $age = 30; $names = ['Andrey', 'Jane',]; $cache = Cache::instance(); //$cache->set('test', $names); //$cache->delete('test'); $data = $cache->get('test'); if(!$data) { $cache->set('test', $names); } debug($data); $this->set(compact('name', 'age', 'names', 'posts')); } } |
| 2 | <?php namespace ishop; class Cache { use TSingletone; public function set($key, $data, $seconds = 3600) { if($seconds) { $content['data'] = $data; $content['end_time'] = time() + $seconds; if(file_put_contents(CACHE.'/'.md5($key).'.txt', serialize($content))) { return true; } } return false; } public function get($key) { $file = CACHE.'/'.md5($key).'.txt'; if(file_exists($file)) { $content = unserialize(file_get_contents($file)); if(time() <= $content['end_time']) { return $content; } unlink($file); } return false; } public function delete($key) { $file = CACHE.'/'.md5($key).'.txt'; if(file_exists($file)) { unlink($file); } } } |
| 3 | <?php namespace ishop; class Db{ use TSingletone; protected function __construct(){ $db = require_once CONF . '/config_db.php'; class_alias('RedBeanPHPR','R'); R::setup($db['dsn'], $db['user'], $db['pass']); if( !R::testConnection() ){ throw new Exception("Нет соединения с БД", 500); } R::freeze(true); if(DEBUG){ R::debug(true, 1); } } } |
| 4 | <?php namespace appcontrollers; class MainController extends AppController { public function indexAction(){ $posts = R::findAll('test'); $post = R::findOne('test', 'id = ?', [2]); $this->setMeta('Главная страница', 'Описание...', 'Ключевики...'); $name = 'John'; $age = 30; $names = ['Andrey', 'Jane',]; $this->set(compact('name', 'age', 'names', 'posts')); } } |
| 5 | <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <?=$this->getMeta();?> </head> <body> <h1>Шаблон DEFAULT</h1> <?=$content;?> <?php $logs = R::getDatabaseAdapter() ->getDatabase() ->getLogger(); debug( $logs->grep( 'SELECT' ) ); ?> </body> </html> |
| 6 | <h1>Hello, world!</h1> <p><?=$name;?></p> <p><?=$age;?></p> <?php debug($names); ?> <?php foreach($posts as $post): ?> <h3><?=$post->title;?></h3> <?php endforeach; ?> <?php namespace appcontrollers; use appmodelsAppModel; use ishopbaseController; class AppController extends Controller{ public function __construct($route){ parent::__construct($route); new AppModel(); } } |
| 7 | { "autoload": { "psr-4": { "ishop": "vendor/ishop/core", "app": "app" } }, "require": { "gabordemooij/redbean": "dev-master" } } <?php namespace appmodels; use ishopbaseModel; class AppModel extends Model{ } |
| 8 | <?php namespace ishop; class Db{ use TSingletone; protected function __construct() { $db = require_once CONF . '/config_db.php'; } } <?php return [ 'dsn' => 'mysql: host=localhost; dbname=ishop2; charset=utf8', 'user' => 'root', 'pass' => '', ]; <?php namespace ishopbase; abstract class Model{ public $attributes = []; public $errors = []; public $rules = []; public function __construct() { } } |
| 9 | public static function matchRoute($url){ foreach(self::$routes as $pattern => $route){ if(preg_match("#{$pattern}#", $url, $matches)){ foreach($matches as $k => $v){ if(is_string($k)){ $route[$k] = $v; } } if(empty($route['action'])){ $route['action'] = 'index'; } if(!isset($route['prefix'])){ $route['prefix'] = ''; }else{ $route['prefix'] .= '\'; } $route['controller'] = self::upperCamelCase($route['controller']); self::$route = $route; return true; } } return false; } |
| 10 | <?php namespace ishop; class Router{ protected static $routes = []; protected static $route = []; public static function add($regexp, $route = []){ self::$routes[$regexp] = $route; } public static function getRoutes(){ return self::$routes; } public static function getRoute(){ return self::$route; } |
| … |
Комментарии