Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
由url构建的PHP模板引擎_Php_Template Engine_Explode_Geturl - Fatal编程技术网

由url构建的PHP模板引擎

由url构建的PHP模板引擎,php,template-engine,explode,geturl,Php,Template Engine,Explode,Geturl,我正在寻找一种动态模板引擎,我想建立的网址。因此,如果我的url类似于localhost/root/this/is/the/path/to/signup.php,那么php代码应该注意this/is/the/path/to目录中的signup.php文件/ 这一次,我使用url数组,如下代码所示: $url = isset($_GET['url']) ? $_GET['url'] : null; $url = rtrim($url, '/'); $url = explode('/', $url)

我正在寻找一种动态模板引擎,我想建立的网址。因此,如果我的url类似于localhost/root/this/is/the/path/to/signup.php,那么php代码应该注意this/is/the/path/to目录中的signup.php文件/

这一次,我使用url数组,如下代码所示:

$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = explode('/', $url);

if (empty($url[0])) {
    $url[0] = "path/startpage.php";
}

if (isset($url[2])) {
    require "path/error.php";
} else if (isset($url[1])) {

    $file1 = 'path/' .$url[0].'/'.$url[1].'/'.$url[1].'.php';

    if (file_exists($file1)) {
        require $file1;
    } else {
        require "error.php";
    }

} else if (isset($url[0])) {

    $file0 = 'path/'.$url[0].'/'.$url[0].'.php';

    if (file_exists($file0)) {
        require $file0;
    } else {
    require "path/error.php";
    }
}
但是有了这个脚本,我必须对每一个案例都这样做,这不是很好。我想有一个解决方案,其中的代码是寻找整个url,去目录,并要求一个错误或文件,如果它存在


您能帮我吗?

Titon\View库很容易处理这个问题:

如果传入模板的路径数组,它将生成动态查找路径

例如:

$view = new Titon\View\View();
$view->addPath('/path/to/views/');
$parsed = $view->run(['path', 'to', 'lookup/folder', 'templateName]);

您还可以查看测试以查看它的运行情况:

当然,您每次都必须检查文件是否存在。我真的不知道你想要更改什么。我想要类似于“$file=path/url”的东西,url应该自动插入。这可能吗?我知道
$\u GET['url']
中的值是多少?