【发布时间】:2017-03-10 22:58:29
【问题描述】:
所以,我创建了一个网上商店系统。这一切都很完美,除了项目总是可以改进的。所以有人告诉我“模板”和“路由”会是很好的改进。我已经为他们两个都写了课程,他们工作得很好!现在,我确实想知道如何将它们结合起来?如何组合这些类,以便需要将来自路由的数据(以确定内容)放置在模板中。我该怎么做?
模板类:
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/用户/退出
有:类 & 方法。
这是一个我已经使用过的简单示例,因为使用此方法不需要显示任何数据。它只会注销已登录的用户。之后,您将被重定向到主页。但是我怎么能将路由用于其他页面呢?例如,无需创建更新文件即可更新一些用户数据?
编辑:
这是我现在使用的页面示例(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();
}
但是,我想要的答案是,如何在此模板中显示来自路由的数据(例如选择用户个人资料),而无需像这样为其创建页面。
【问题讨论】:
-
因此,在上面的示例 URL 中,“用户”是类,“注销”是方法。您的模板类将在注销方法中使用。在该方法中,您可以将其称为
new Template(),并将视图的文件位置作为参数传递给它。也许我没有正确遵循?这就是你想知道的吗? -
我已经编辑了我的问题,希望它对你来说更清楚一点
标签: php oop url routing templating