所以,我创建了一个网上商店系统。这一切都很完美,除了项目总是可以改进。所以有人告诉我'模板'和'路由'将是很好的改进。我为他们两人写了课,他们工作得很好!现在,我想知道如何将它们结合起来?如何组合这些类,以便需要将来自路由的数据(以确定内容)放在模板中。我该怎么做?
模板类:
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访问我的路线,如下所示:
http://localhost:8080/Webshop/Route/ 用户 / 注销
使用: Class &amp;的方法
这是我已经使用的一个简单示例,因为不需要使用此方法显示数据。它只会注销登录的用户。之后,您将被重定向到主页。但是我如何使用路由到其他页面?例如,更新一些用户数据而不必创建更新文件?
这是我现在使用的页面示例(index.php):
<?php
/*
** @description Includes config.php once so that we can use classes, defines etcetera.
*/
require_once('core/preferences/config.php');
/*
** @description Instanciate new route object.
*/
$route = new Route();
/*
** @description Check if a route isset. When not, continue, else: run route
*/
if(!$route->checkUrl())
{
/*
** @description Instanciate new template object.
*/
$template = new Template(TEMPLATES_PATH .'/camerashop24.tpl.html');
/*
** @description Assign values.
*/
$template->assign('title', 'Home');
$template->assign('root', '');
$template->quickLoad('loginout', 'loginout');
$template->quickLoad('title_block', 'title_block');
$template->quickLoad('cart','cart');
$template->quickLoad('menu', 'menu');
$template->assign('page', 'Home');
$breadcrumbs = new Breadcrumbs($_SERVER["REQUEST_URI"],'');
$template->assign('breadcrumbs', $breadcrumbs->data());
$content = "";
foreach(explode(",",Config::get('settings/front_page_cat')) as $item) {
$content .= "<div id='blue-box' class='blue-box'><h2 style='color: white;'>" . strtoupper($item) . "</h2></div>";
$category = new Category();
$category->find($item);
if($category->exists($item)){
foreach (explode(",",$category->data()->products) as $item) {
$product = new Product($item);
$product->find($item);
$content .= '<a href="Product/' . $product->data()->type . '">' . $product->showProduct($product->data()->type,$product->data()->name,$product->data()->price) . '</a>';
}
}
}
$template->assign('text', $content);
$template->quickLoad('footer','footer');
/*
** @description Showing content.
*/
$template->show();
}
但是,我想要的答案是,如何在此模板中显示路由中的数据(例如选择用户配置文件),而不必为此创建页面。
答案 0 :(得分:1)
我认为你必须修改你的路由类。理想情况下,您希望路由类能够告诉它路由应该使用哪个控制器。您选择的控制器将充当处理中间人。每个控制器的方法都代表一条路线,如下所示:
/**
* 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();
}
}