mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-30 09:10:38 +02:00
Fixed 'Home' link in header breadcrumbs
This commit is contained in:
@@ -21,6 +21,7 @@ class TwigProvider
|
||||
ViewFunctions\Markdown::class,
|
||||
ViewFunctions\ParentDir::class,
|
||||
ViewFunctions\SizeForHumans::class,
|
||||
ViewFunctions\Url::class,
|
||||
];
|
||||
|
||||
/** @var Container The application container */
|
||||
|
23
app/ViewFunctions/Url.php
Normal file
23
app/ViewFunctions/Url.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\ViewFunctions;
|
||||
|
||||
class Url extends ViewFunction
|
||||
{
|
||||
/** @var string The function name */
|
||||
protected $name = 'url';
|
||||
|
||||
/**
|
||||
* Return the URL for a given path.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __invoke(string $path): string
|
||||
{
|
||||
$url = dirname($_SERVER['SCRIPT_NAME']) . '/' . ltrim($path, '/');
|
||||
|
||||
return '/' . trim($url, '/');
|
||||
}
|
||||
}
|
@@ -1,7 +1,7 @@
|
||||
<header id="header" class="bg-blue-600 shadow sticky top-0">
|
||||
<div class="container flex flex-col justify-between items-center mx-auto p-4 md:flex-row">
|
||||
<div class="font-mono text-white text-sm tracking-tight mb-2 md:my-1">
|
||||
<a href="/" class="hover:underline">Home</a>
|
||||
<a href="{{ url('/') }}" class="hover:underline">Home</a>
|
||||
|
||||
{% if path %}
|
||||
{% for name, path in breadcrumbs(path) %}
|
||||
|
29
tests/ViewFunctions/UrlTest.php
Normal file
29
tests/ViewFunctions/UrlTest.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\ViewFunctions;
|
||||
|
||||
use App\ViewFunctions\Url;
|
||||
use Tests\TestCase;
|
||||
|
||||
class UrlTest extends TestCase
|
||||
{
|
||||
public function test_it_can_return_a_url(): void
|
||||
{
|
||||
$url = new Url($this->container, $this->config);
|
||||
|
||||
$this->assertEquals('/', $url('/'));
|
||||
$this->assertEquals('/some/path', $url('some/path'));
|
||||
$this->assertEquals('/some/file.test', $url('some/file.test'));
|
||||
}
|
||||
|
||||
public function test_it_can_return_a_url_in_a_subdirectory(): void
|
||||
{
|
||||
$_SERVER['SCRIPT_NAME'] = '/some/dir/index.php';
|
||||
|
||||
$url = new Url($this->container, $this->config);
|
||||
|
||||
$this->assertEquals('/some/dir', $url('/'));
|
||||
$this->assertEquals('/some/dir/some/path', $url('some/path'));
|
||||
$this->assertEquals('/some/dir/some/file.test', $url('some/file.test'));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user