Метод isAbsolute класса Path
Метод isAbsolute класса Path возвращает
true, если путь является абсолютным, и
false, если путь относительный. Параметров
метод не принимает. Абсолютным считается путь,
который начинается от корня файловой системы,
например /var/www или C:\Projects.
Синтаксис
public function isAbsolute(): bool
Пример
Давайте проверим абсолютный путь:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Filesystem\Path;
class PathController extends AbstractController
{
#[Route('/path', name: 'path_index')]
public function index(): Response
{
$path = new Path('/var/www/project');
$res = $path->isAbsolute();
return new Response(var_export($res, true));
}
}
?>
Результат выполнения кода:
true
Пример
Давайте проверим относительный путь:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Filesystem\Path;
class PathController extends AbstractController
{
#[Route('/path', name: 'path_index')]
public function index(): Response
{
$path = new Path('article/user');
$res = $path->isAbsolute();
return new Response(var_export($res, true));
}
}
?>
Результат выполнения кода:
false
Пример
Давайте проверим путь, начинающийся от домашней директории:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Filesystem\Path;
class PathController extends AbstractController
{
#[Route('/path', name: 'path_index')]
public function index(): Response
{
$path = new Path('~/project');
$res = $path->isAbsolute();
return new Response(var_export($res, true));
}
}
?>
Результат выполнения кода:
true
Смотрите также
-
класс
Path,
который предоставляет методы для работы с путями -
метод
isRelative,
который проверяет, является ли путь относительным -
метод
makeAbsolute,
который преобразует относительный путь в абсолютный -
метод
canonicalize,
который приводит путь к каноническому виду