Skip to content

Commit 2632b4c

Browse files
committed
feat(routing): implement routing helper facade
1 parent c6cc048 commit 2632b4c

File tree

7 files changed

+39
-17
lines changed

7 files changed

+39
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules/
33
npm-debug.log
44
yarn-error.log
5+
.vscode

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ Options:
4949
5050
By default the package automatically adds a route for nuxt pages.
5151
52-
`config('nuxt.prefix').'/{path?}'`
52+
`Nuxt::route('/{path?}')->where('path', '.*')->name('nuxt');`
5353
54-
This route is named `'nuxt'` and can be used to redirect to using Laravels route helper.
54+
This route is named `'nuxt'` and can be used with Laravels route helper.
5555
5656
```php
5757
route('nuxt');
@@ -65,12 +65,15 @@ You can disable/enable automatic routing with the `'routing'` setting in `config
6565
6666
## Manual Routing
6767
68-
If you would like to manually define routes for nuxt you can use the included Controller.
68+
The package provides a simple facade which might be used to register nuxt routes.
69+
Routes will automatically be prefixed with the configured path and a controller will be attached which handles internal redirection to nuxt.
70+
The method returns a `Illuminate\Routing\Route` instance and may be used as normal.
71+
72+
In `routes/web.php`:
6973
7074
```php
7175

72-
use M2S\LaravelNuxt\Http\Controllers\NuxtController;
76+
use M2S\LaravelNuxt\Facades\Nuxt;
7377

74-
// Be sure to use the defined 'prefix', as nuxt uses this internally for route resolution
75-
Route::get('app/example/route', NuxtController::class);
78+
Nuxt::route('example/route')->name('nuxt.example');
7679
```

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
"laravel": {
4343
"providers": [
4444
"M2S\\LaravelNuxt\\LaravelNuxtServiceProvider"
45-
]
45+
],
46+
"aliases": {
47+
"Nuxt": "M2S\\LaravelNuxt\\Facades\\Nuxt"
48+
}
4649
}
4750
}
4851
}

phpmd.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
<exclude-pattern>*/views/*</exclude-pattern>
77
<rule ref="rulesets/cleancode.xml/BooleanArgumentFlag" />
88
<rule ref="rulesets/cleancode.xml/ElseExpression" />
9-
<rule ref="rulesets/cleancode.xml/StaticAccess" />
9+
<rule ref="rulesets/cleancode.xml/StaticAccess">
10+
<properties>
11+
<property name="exceptions" value="\Illuminate\Support\Facades\Route" />
12+
</properties>
13+
</rule>
1014
<rule ref="rulesets/codesize.xml" />
1115
<rule ref="rulesets/controversial.xml/Superglobals" />
1216
<rule ref="rulesets/controversial.xml/CamelCaseClassName" />

routes/nuxt.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
<?php
22

3-
use Illuminate\Support\Facades\Route;
4-
use M2S\LaravelNuxt\Http\Controllers\NuxtController;
3+
use M2S\LaravelNuxt\Facades\Nuxt;
54

6-
$prefix = rtrim(config('nuxt.prefix'), '/');
7-
8-
Route::get(
9-
"$prefix/{path?}",
10-
NuxtController::class
11-
)->name('nuxt')->where('path', '.*');
5+
Nuxt::route('{path?}')->name('nuxt')->where('path', '.*');

src/Console/Commands/InstallCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public function __construct()
5959
public function handle()
6060
{
6161
$source = base_path($this->argument('source'));
62-
$prefixConfig = trim(config('nuxt.prefix'), '/');
6362
$yarn = $this->option('yarn') ?: $this->confirm('Use yarn package manager?');
6463
$typescript = $this->option('typescript') ?: $this->confirm('Use typescript runtime?');
64+
$prefixConfig = trim(config('nuxt.prefix'), '/');
6565
$prefix = trim($this->option('prefix') ?: $prefixConfig, '/');
6666

6767
$this->installNuxtRemote($source, $yarn);

src/Facades/Nuxt.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace M2S\LaravelNuxt\Facades;
4+
5+
use Illuminate\Support\Facades\Route;
6+
use M2S\LaravelNuxt\Http\Controllers\NuxtController;
7+
8+
class Nuxt
9+
{
10+
public static function route(string $path)
11+
{
12+
return Route::get(
13+
'/'.trim(config('nuxt.prefix'), '/').'/'.trim($path, '/'),
14+
NuxtController::class
15+
);
16+
}
17+
}

0 commit comments

Comments
 (0)