Skip to content

Commit 6773d5b

Browse files
authored
Merge pull request #18 from Laravel-Backpack/add-option-to-dont-setup-routes
ability to customize package routes
2 parents 1181a27 + 8a172e8 commit 6773d5b

File tree

5 files changed

+45
-14
lines changed

5 files changed

+45
-14
lines changed

config/language-switcher.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
return [
4+
// when `false` developer should setup their own route for language switcher
5+
// check the default route at /vendor/backpack/language-switcher/routes/language-switcher.php
6+
'setup_routes' => true,
7+
8+
// when true, we will add the route prefix on the language-switcher route.
9+
// eg: https://domain.com/admin/set-locale/{locale} instead of https://domain.com/set-locale/{locale}
10+
'use_backpack_route_prefix' => false,
11+
];

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Try it right now, in [our online demo](https://demo.backpackforlaravel.com/admin
2121

2222
```bash
2323
composer require backpack/language-switcher
24+
25+
# optional: publish the config file
26+
php artisan vendor:publish --provider="Backpack\LanguageSwitcher\LanguageSwitcherServiceProvider" --tag="config"
2427
```
2528

2629
2) Add the middleware to backpack config `config/backpack/base.php`:
@@ -77,6 +80,12 @@ protected $middlewareGroups = [
7780
],
7881
```
7982
83+
### Can I customize the endpoint routes ?
84+
**Yes!**
85+
You can do it by publishing the config file `php artisan vendor:publish --provider="Backpack\LanguageSwitcher\LanguageSwitcherServiceProvider" --tag="config"`.
86+
There you can totally disable the package route and register your own, or change some behavior related with display urls.
87+
88+
Please take caution to protect the endpoint with throttling or any other security measure if you overwrite the routes. The default package route uses: `'throttle:60,1'`
8089
8190
## Notes
8291

resources/views/language-switcher.blade.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@
1414
@endif
1515
</a>
1616
<ul class="dropdown-menu dropdown-menu-right dropdown-menu-end" style="right: 0">
17+
@php
18+
$useAdminPrefix = config('backpack.language-switcher.use_backpack_route_prefix');
19+
@endphp
1720
@foreach(config('backpack.crud.locales', []) as $locale => $name)
1821
<li>
19-
<a class="dropdown-item {{ $locale === $helper->getCurrentLocale() ? 'active disabled' : '' }}" href="{{ route('language-switcher.locale', $locale) }}">
22+
<a class="dropdown-item {{ $locale === $helper->getCurrentLocale() ? 'active disabled' : '' }}" href="{{ route('language-switcher.locale', [
23+
'locale' => $useAdminPrefix ? $locale : null,
24+
'backpack_prefix' => $useAdminPrefix ? config('backpack.base.route_prefix') : 'set-locale',
25+
'setLocale' => $useAdminPrefix ? 'set-locale' : $locale
26+
])}}">
2027
@if($flags ?? true)
2128
<span class="nav-link-icon" style="width: fit-content">
2229
<x-dynamic-component component="flag-{{ $helper->getFlagOrFallback($locale) }}" style="width: 1.5rem" />

routes/language-switcher.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
2-
3-
use Backpack\LanguageSwitcher\Http\Controllers\LanguageSwitcherController;
4-
2+
use Illuminate\Support\Facades\Route;
53
/*
64
|--------------------------------------------------------------------------
75
| Backpack\LanguageSwitcher Routes
@@ -11,11 +9,15 @@
119
| handled by the Backpack\LanguageSwitcher package.
1210
|
1311
*/
14-
Route::group([
15-
'namespace' => 'Backpack\LanguageSwitcher\Http\Controllers',
16-
'middleware' => ['web', 'throttle:60,1'],
17-
], function () {
18-
// set locale
19-
Route::any('set-locale/{locale}', [LanguageSwitcherController::class, 'setLocale'])
20-
->name('language-switcher.locale');
21-
});
12+
if(config('backpack.language-switcher.setup_routes', true)) {
13+
Route::group([
14+
'middleware' => ['web', 'throttle:60,1'],
15+
], function () {
16+
// set locale
17+
Route::any('{backpack_prefix?}/{setLocale}/{locale?}', [\Backpack\LanguageSwitcher\Http\Controllers\LanguageSwitcherController::class, 'setLocale'])
18+
->name('language-switcher.locale')
19+
->whereIn('setLocale', array_merge(['set-locale'],array_keys(config('backpack.crud.locales'))))
20+
->whereIn('backpack_prefix', ['set-locale', config('backpack.base.route_prefix')]);
21+
});
22+
}
23+

src/Http/Controllers/LanguageSwitcherController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ class LanguageSwitcherController extends Controller
1616
/**
1717
* Set's the app locale
1818
*/
19-
public function setLocale(string $locale): Redirector | RedirectResponse
19+
public function setLocale(?string $backpackPrefix = null, ?string $setLocale = null, ?string $locale = null): Redirector | RedirectResponse
2020
{
21+
$locale ??= $setLocale;
22+
2123
if (in_array($locale, array_keys(config('backpack.crud.locales')))) {
2224
Session::put('backpack.language-switcher.locale', $locale);
2325
}
2426

25-
return redirect(url()->previous());
27+
return redirect()->back();
2628
}
2729
}

0 commit comments

Comments
 (0)