Php 如何结合路由和模板?
所以,我创建了一个网络商店系统。除了项目总是可以改进之外,所有这些都非常有效。所以有人告诉我,“模板”和“路由”将是很好的改进。我为他们两个都写过课,他们都很好!现在,我真的想知道如何组合它们?如何组合这些类,以便路由中的数据(用于确定内容)需要放置在模板中。我该怎么做 模板类:Php 如何结合路由和模板?,php,oop,url,routing,templating,Php,Oop,Url,Routing,Templating,所以,我创建了一个网络商店系统。除了项目总是可以改进之外,所有这些都非常有效。所以有人告诉我,“模板”和“路由”将是很好的改进。我为他们两个都写过课,他们都很好!现在,我真的想知道如何组合它们?如何组合这些类,以便路由中的数据(用于确定内容)需要放置在模板中。我该怎么做 模板类: class Template { private $assignedValues = array(); private $tpl; /* **
class Template
{
private $assignedValues = array();
private $tpl;
/*
** @description Creates one single instance of itself and checks whether the template file exists.
** @param $path [string] This is the path to the template
*/
public function __construct($_path = '')
{
if(!empty($_path)){
if(file_exists($_path)){
$this->tpl = file_get_contents($_path);
}
else{
echo '<b>Template Error:</b> File Inclusion Error.';
}
}
}
/*
** @description Assign a value to a part in the template.
** @param $_searchString [string] This is the part in the template that needs to be replaced
** @param $_replaceString [string] This is the code/text that will replace the part in the template
*/
public function assign($_searchString, $_replaceString)
{
if(!empty($_searchString)){
$this->assignedValues[strtoupper($_searchString)] = $_replaceString;
}
}
/*
** @description Shows the final result of the page.
*/
public function show()
{
if(count($this->assignedValues > 0)){
foreach ($this->assignedValues as $key => $value) {
$this->tpl = str_replace('{'.$key.'}', $value, $this->tpl);
}
}
echo $this->tpl;
}
/*
** @description Quickly load a part of the page
** @param $quickLoad [string] This is the name of the file that will be loaded and assigned
** @param $_searchString [string] This is the part in the template that needs to be replaced
*/
public function quickLoad($_searchString, $part)
{
if(file_exists(INCLUDES.DS.$part.'.php')){
$this->assign($_searchString,include(INCLUDES.DS.$part.'.php'));
}
else{
return "That file does not exist!";
}
}
}
class Route
{
protected $controller = 'App';
protected $method = 'Call';
protected $params = [];
/*
** @description Loads the classes and methods which are referred to.
*/
public function __construct()
{
$url = $this->parseUrl();
if($this->checkUrl())
{
unset($url[0]);
if(isset($url[1]))
{
if (file_exists('core/classes/' . $url[1] . '.class.php'))
{
$this->controller = $url[1];
unset($url[1]);
}
}
require_once('core/classes/' . $this->controller . '.class.php');
$this->controller = new $this->controller;
if (isset($url[2]))
{
if (method_exists($this->controller, $url[2]))
{
$this->method = $url[2];
unset($url[2]);
}
}
$this->params = $url ? array_values($url) : [];
$this->arrayUrl($this->params);
call_user_func_array([$this->controller, $this->method], $this->params);
}
}
/*
** @description Check whether the URL part contains a string
*/
public function checkUrl($index = '0',$value = 'Route'){
$url = $this->parseUrl();
if($url[$index] == $value){
return true;
}
return false;
}
/*
** @description Splits the url into pieces.
*/
protected function parseUrl()
{
if(isset($_GET['url']))
{
return $url = explode('/', filter_var(rtrim(urldecode($_GET['url']), '/'), FILTER_SANITIZE_URL));
}
}
/*
** @description Sets arrays in routes.
*/
protected function arrayUrl($params = array())
{
foreach($params as $index => $param)
{
if (preg_match('/>/',$param))
{
$newParam = explode('>', $param);
unset($this->params[$index]);
$this->params['fields'][$newParam[0]] = $newParam[1];
}
else{
unset($this->params[$index]);
$this->params[] = $param;
}
}
print_r($this->params);
}
}
我可以通过如下URL访问我的路线:
用户/注销使用:Class&方法 这是一个我已经使用过的简单示例,因为不需要使用此方法显示数据。它仅注销已登录的用户。之后,您将被重定向到主页。但我如何使用其他页面的路由?例如,在不创建更新文件的情况下更新某些用户数据 编辑: 这是我现在使用的一个页面示例(index.php):
我想你必须修改你的路由类。理想情况下,您希望routing类能够告诉它路由应该使用什么控制器。您选择的控制器将作为处理的中间人。每个控制器的方法将表示一条路由,如下所示:
/**
* URL = www.test.com/index.php/home
* URL = www.test.com/index.php/about
*
* ROUTE Class:
* - should define routes. Example:
*
* $route["/home"] = MainController/home
* $route["/about"] = MainController/about
* $route["/*"] = MainController/*
*
*/
class MainController
{
public function home()
{
$template = new Template(TEMPLATES_PATH . 'home.tpl.html');
$template->show();
}
public function about()
{
$template = new Template(TEMPLATES_PATH . 'about.tpl.html');
$template->show();
}
}
因此,在上面的示例URL中,“User”是类,“logout”是方法。您的模板类将用于注销方法。在该方法中,您可以将其称为newtemplate()
将视图的文件位置作为参数传递给它。也许我跟不上?这就是你想知道的吗?我编辑了我的问题,希望它能让你更清楚一点
/**
* URL = www.test.com/index.php/home
* URL = www.test.com/index.php/about
*
* ROUTE Class:
* - should define routes. Example:
*
* $route["/home"] = MainController/home
* $route["/about"] = MainController/about
* $route["/*"] = MainController/*
*
*/
class MainController
{
public function home()
{
$template = new Template(TEMPLATES_PATH . 'home.tpl.html');
$template->show();
}
public function about()
{
$template = new Template(TEMPLATES_PATH . 'about.tpl.html');
$template->show();
}
}