Php 检查路径中的文件夹并删除url的其余部分
我需要将两条路径剥离回“视图”文件夹,然后检查该文件夹中是否存在文件 视图/[Controller}/{Action}/{ID} 区域/视图/[Controller}/{Action}/{ID} 我正在寻找一种从url中删除视图右侧所有内容的方法/使用和的组合Php 检查路径中的文件夹并删除url的其余部分,php,Php,我需要将两条路径剥离回“视图”文件夹,然后检查该文件夹中是否存在文件 视图/[Controller}/{Action}/{ID} 区域/视图/[Controller}/{Action}/{ID} 我正在寻找一种从url中删除视图右侧所有内容的方法/使用和的组合 也许是这样的: $url = 'Areas/Views/[Controller}/{Action}/{ID}'; $segmentedUrl = explode('/',$url); $viewsPosition = array_sear
也许是这样的:
$url = 'Areas/Views/[Controller}/{Action}/{ID}';
$segmentedUrl = explode('/',$url);
$viewsPosition = array_search('Views', $segmentedUrl);
$leftPart = array_slice($segmentedUrl, 0, $viewsPosition+1);
var_dump($leftPart);
删除是什么意思?您对路径了解多少?路径输入的位置/方式或可能包含的内容?我想知道请求的视图是在视图中还是在区域/视图中?我只需要url的第一部分。
$url = 'Areas/Views/[Controller}/{Action}/{ID}';
$segmentedUrl = explode('/',$url);
$viewsPosition = array_search('Views', $segmentedUrl);
$leftPart = array_slice($segmentedUrl, 0, $viewsPosition+1);
var_dump($leftPart);
<?php
class Path{
protected $originalPath;
protected $path; // only this path gets manipulated for method chaining reasons
public function setPath($path)
{
$this->originalPath = $path;
$this->path = $path;
}
public function getPartial($partial)
{
if(!isset($this->originalPath))
throw new Exception('No Path set');
// Get the position of partial in our path
$rawPos = strpos($this->path, $partial);
if($rawPos === false)
{
throw new Exception('The Partial: '.$partial. 'was not found within the path');
}
else
{
// If we found the partial let's add the length of the given
// string to figure the actual position
$pos = $rawPos + strlen($partial);
$this->path = substr($this->path, 0, $pos);
return $this;
}
}
public function find($file)
{
$path = $this->path;
$this->rewindPath();
if(is_file($path . '/' . $file))
return true;
return false;
}
protected function rewindPath()
{
$this->path = $this->originalPath;
}
}
// Instantiate Class
$path = new Path();
$path->setPath('Views/[Controller}/{Action}/{ID}');
var_dump($path->getPartial('Views')->find('test.txt'));
$path->setPath('Test/Views/[Controller}/{Action}/{ID}');
var_dump($path->getPartial('Views')->find('test.txt'));
?>