Skip to content

Commit be56398

Browse files
authored
Merge pull request #225 from hydephp/add-a-link-component
Add a link component to resolve routes automatically
2 parents abcf759 + da06c2c commit be56398

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

docs/architecture-concepts.md

+51
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,54 @@ author:
9292

9393
Lorem ipsum dolor sit amet, etc.
9494
```
95+
96+
97+
## Automatic Routing
98+
99+
>info This covers an intermediate topic which is not required for basic usage, but is useful if you want to use the framework to design custom Blade templates.
100+
101+
### High-level overview
102+
103+
If you've ever worked in a MVC framework, you are probably familiar with the concept of routing. And you are probably also familiar with how boring and tedious it can be. Hyde takes the pain out of routing through the Hyde Autodiscovery process.
104+
105+
Internally, when booting the Hyde application, Hyde will automatically discover all of the content files in the source directory and create a routing index for them. This index works as a two-way link between source files and compiled files.
106+
107+
Don't worry if this sounds complex, as the key takeaway is that the index is created and maintained automatically. There is currently no way to manually add or remove files from the index. Making it function more like a source map than a proper router. Nevertheless, the routing system provides several helpers that you can optionally use in your Blade views to automatically resolve relative links and other useful features.
108+
109+
### Accessing routes
110+
111+
Each route in your site is represented by a Route object. It's very easy to get a Route object instance from the Router's index. There are a few ways to do this, but most commonly you'll use the Route facade's `get()` method where you provide a route key, and it will return the Route object. The route key is generally `<output-directory/slug>`. Here are some examples:
112+
113+
```php
114+
// Source file: _pages/index.md/index.blade.php
115+
// Compiled file: _site/index.html
116+
Route::get('index')
117+
118+
// Source file: _posts/my-post.md
119+
// Compiled file: _site/posts/my-post.html
120+
Route::get('posts.my-post')
121+
122+
// Source file: _docs/readme.md
123+
// Compiled file: _site/docs/readme.html
124+
Route::get('docs.readme')
125+
```
126+
127+
### Using the `x-link` component
128+
129+
When designing Blade layouts it can be useful to use the `x-link` component to automatically resolve relative links.
130+
131+
You can of course, use it just like a normal anchor tag like so:
132+
```blade
133+
<x-link href="index.html">Home</x-link>
134+
```
135+
136+
But where it really shines is when you supply a route. This will then resolve the proper relative link, and format it to use pretty URLs if your site is configured to use them.
137+
138+
```blade
139+
<x-link href="Route::get('index')">Home</x-link>
140+
```
141+
142+
You can of course, also supply extra attributes like classes:
143+
```blade
144+
<x-link href="Route::get('index')" class="btn btn-primary">Home</x-link>
145+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a {{ $attributes->merge(['href' => $href]) }}>{!! $slot !!}</a>

packages/framework/src/HydeServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Hyde\Framework\Models\Pages\MarkdownPage;
1010
use Hyde\Framework\Models\Pages\MarkdownPost;
1111
use Hyde\Framework\Services\AssetService;
12+
use Hyde\Framework\Views\Components\LinkComponent;
13+
use Illuminate\Support\Facades\Blade;
1214
use Illuminate\Support\ServiceProvider;
1315

1416
/**
@@ -79,6 +81,8 @@ public function boot(): void
7981
$this->publishes([
8082
Hyde::vendorPath('resources/views/homepages/welcome.blade.php') => Hyde::path('_pages/index.blade.php'),
8183
], 'hyde-welcome-page');
84+
85+
Blade::component('link', LinkComponent::class);
8286
}
8387

8488
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Hyde\Framework\Views\Components;
4+
5+
use Hyde\Framework\Hyde;
6+
use Illuminate\Support\Facades\View;
7+
use Illuminate\View\Component;
8+
9+
class LinkComponent extends Component
10+
{
11+
public string $href;
12+
13+
public function __construct(string $href)
14+
{
15+
$this->href = Hyde::relativeLink($href, View::shared('currentPage') ?? '');
16+
}
17+
18+
public function render(): \Illuminate\Contracts\View\View
19+
{
20+
return view('hyde::components.link');
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Hyde\Framework\Testing\Unit\Views\Components;
4+
5+
use Hyde\Testing\TestCase;
6+
use Illuminate\Support\Facades\Blade;
7+
use Illuminate\Support\Facades\View;
8+
9+
/**
10+
* @covers \Hyde\Framework\Views\Components\LinkComponent
11+
*/
12+
class LinkComponentTest extends TestCase
13+
{
14+
public function test_link_component_can_be_rendered()
15+
{
16+
$this->assertEquals('<a href="foo">bar</a>', rtrim(Blade::render('<x-link href="foo">bar</x-link>')));
17+
}
18+
19+
public function test_link_component_can_be_rendered_with_route()
20+
{
21+
$route = \Hyde\Framework\Facades\Route::get('index');
22+
$this->assertEquals('<a href="index.html">bar</a>', rtrim(
23+
Blade::render('<x-link href="'.$route.'">bar</x-link>')));
24+
}
25+
26+
public function test_link_component_can_be_rendered_with_route_for_nested_pages()
27+
{
28+
View::share('currentPage', 'foo/bar');
29+
$route = \Hyde\Framework\Facades\Route::get('index');
30+
$this->assertEquals('<a href="../index.html">bar</a>', rtrim(
31+
Blade::render('<x-link href="'.$route.'">bar</x-link>')));
32+
}
33+
}

0 commit comments

Comments
 (0)