<?php
namespace startmvc\core;
use startmvc\core\Request;use startmvc\core\Db;
use startmvc\core\Loader;
use startmvc\core\View;
abstract class Controller
{
public $conf;
protected $db;
public $assign;
protected $view;
public function __construct()
{
$this->conf = include CONFIG_PATH . 'common.php';
if($this->conf['db_auto_connect']){
$dbConf = include CONFIG_PATH . '/database.php';
if ($dbConf['default'] != '') {
$this->db= new Db($dbConf['connections'][$dbConf['default']]);
}
}
$this->view = new View();
}
protected function model($model, $module = MODULE)
{
$model = APP_NAMESPACE.'\\' . $module . '\\'. 'model\\' . $model . 'Model';
return Loader::getInstance($model);
}
protected function url($url)
{
$url = $url . $this->conf['url_suffix'];
if ($this->conf['urlrewrite']) {
$url = '/' . $url;
} else {
$url = '/index.php/' . $url;
}
return str_replace('%2F', '/', urlencode($url));
}
protected function assign($name=[], $data='')
{
$this->view->assign($name, $data);
}
protected function display($tplfile='',$data=[])
{
echo $this->view->display($tplfile,$data);
}
public function content($content)
{
header('Content-Type:text/plain; charset=utf-8');
echo $content;
}
protected function success($msg='',$url='',$data=[],$ajax=false)
{
$this->response(1,$msg,$url,$data,$ajax);
}
protected function error($msg='',$url='',$data=[],$ajax=false)
{
$this->response(0,$msg,$url,$data,$ajax);
}
protected function response($code='',$msg='',$url='',$data=[],$ajax=false)
{
if($ajax || Request::isAjax()){
$data=[
'code'=>$code 'msg'=>$msg,
'url'=>$url,
'data'=>$data,
];
$this->json($data);
}else{
include __DIR__.DS.'tpl/jump.php';
exit();
}
}
protected function json($data)
{
header('Content-Type:application/json; charset=utf-8');
exit(json_encode($data, JSON_UNESCAPED_UNICODE));
}
protected function redirect($url='')
{
$url=$url?:'/';
header('location:' . $url);
exit();
}
protected function notFound()
{
header("HTTP/1.1 404 Not Found");
header("Status: 404 Not Found");
}
public function __call($fun, $arg)
{
$this->notFound();
}
}