Fixed 'Home' link in header breadcrumbs

This commit is contained in:
Chris Kankiewicz
2020-01-26 00:54:29 -07:00
parent 2f87c29e5d
commit 9ad8c54d49
4 changed files with 54 additions and 1 deletions

View File

@@ -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
View 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, '/');
}
}

View File

@@ -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) %}

View 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'));
}
}