Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit bce9f11

Browse files
committed
fixed a couple of issues, add more options to config
1 parent 30a2574 commit bce9f11

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ Route::group(['middleware' => ['role:admin','perm:access_backend']], function ()
6161
```
6262

6363
- if `action` is added, the page `url & prefix` wont be slugged.
64-
- `action` **default namespace** is `App\Http\Controllers`, so all your controllers should be available under that.
6564

6665
# ToDo
6766

src/Traits/NavigationTrait.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function getUrl($name, $code)
1919
$url = $this->routeLink($name, $code);
2020

2121
if (!$url) {
22-
// page is hardcoded
23-
return LaravelLocalization::getLocalizedURL($code);
22+
// page is hardcoded, means its not saved in the db (ex.php artisan make:auth routes)
23+
return LaravelLocalization::getLocalizedURL($code, null, [], true);
2424
}
2525

2626
// if we have a saved params
@@ -34,7 +34,7 @@ public function getUrl($name, $code)
3434

3535
// no params
3636
return LaravelLocalization::getLocalizedURL(
37-
$code, url($this->rmvNonUsedParams($url)), [], true
37+
$code, url($this->rmvUnUsedParams($url)), [], true
3838
);
3939
}
4040

@@ -143,7 +143,22 @@ protected function routeLink($name, $code)
143143
$code = app('laravellocalization')->getDefaultLocale();
144144
}
145145

146-
return array_get($file, "$name.$code");
146+
// check if we have a link according to that "routeName & code"
147+
$search = array_get($file, "$name.$code");
148+
149+
// if notFound, then either redir to home or abort
150+
if (!$search) {
151+
switch (config('simpleMenu.unFoundLocalizedRoute')) {
152+
case 'home':
153+
return '/';
154+
break;
155+
case 'error':
156+
return '/404';
157+
break;
158+
}
159+
}
160+
161+
return $search;
147162
}
148163

149164
/**
@@ -160,7 +175,7 @@ protected function getParams($url, $params)
160175
$url = preg_replace('/\{'.preg_quote($key).'(\?)?\}/', $value, $url);
161176
}
162177

163-
return $this->rmvNonUsedParams($url);
178+
return $this->rmvUnUsedParams($url);
164179
}
165180

166181
/**
@@ -171,7 +186,7 @@ protected function getParams($url, $params)
171186
*
172187
* @return [type] [description]
173188
*/
174-
protected function rmvNonUsedParams($url)
189+
protected function rmvUnUsedParams($url)
175190
{
176191
return preg_replace('/\{.*\}/', '', $url);
177192
}

src/Traits/RoutesTrait.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Facades\Route;
77
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
8+
use Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter;
9+
use Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect;
810

911
trait RoutesTrait
1012
{
@@ -21,8 +23,12 @@ public function createRoutes()
2123
{
2224
Route::group([
2325
'prefix' => LaravelLocalization::setLocale(),
24-
'middleware' => ['web', 'localeSessionRedirect', 'localizationRedirect'],
25-
'namespace' => 'App\Http\Controllers',
26+
'middleware' => [
27+
'web',
28+
LocaleSessionRedirect::class,
29+
LaravelLocalizationRedirectFilter::class,
30+
],
31+
'namespace' => config('simpleMenu.pagesControllerNS'),
2632
], function () {
2733
$this->utilCheck();
2834
}
@@ -140,8 +146,13 @@ protected function createRoutesList($action, $page, $routeName)
140146
foreach ($this->localeCodes as $code) {
141147
$url = $page->getTranslation('url', $code);
142148

143-
if (config('simpleMenu.useTitleForUrl') && is_null($page->getTranslation('url', $code))) {
144-
$url = slugfy($page->getTranslation('title', $code));
149+
if (is_null($page->getTranslation('url', $code))) {
150+
if (config('simpleMenu.mainRouteName') == $routeName) {
151+
$url = '/';
152+
}
153+
if (config('simpleMenu.useTitleForUrl')) {
154+
$url = slugfy($page->getTranslation('title', $code));
155+
}
145156
}
146157

147158
$prefix = $action !== null ? $page->getTranslation('prefix', $code) : slugfy($page->getTranslation('prefix', $code));

src/config/simpleMenu.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
return [
4+
/*
5+
* the name of the main route '/',
6+
* make sure you have a row in ur db with a `route_name = the below`
7+
*/
8+
'mainRouteName'=> 'home',
9+
410
/*
511
* the menu list classes to be used for "render()"
612
*/
@@ -21,7 +27,18 @@
2127
'routeListPath' => storage_path('logs/simpleMenu.php'),
2228

2329
/*
24-
* if url is empty should we use a slugged title instead ?
30+
* if url is empty (ex.'/') should we use a slugged title instead ?
2531
*/
2632
'useTitleForUrl' => false,
33+
34+
/*
35+
* what happens when a route is available in one locale "en" but not in another "fr"
36+
* add either 'home' or 'error'
37+
*/
38+
'unFoundLocalizedRoute' => 'error',
39+
40+
/*
41+
* pages controller namespace
42+
*/
43+
'pagesControllerNS'=> 'App\Http\Controllers',
2744
];

src/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function trimfy($value, $limit = 100, $end = ' ...')
88
{
99
$value = strip_tags($value);
1010

11-
if (crntLang() != 'en') {
11+
if (config('app.locale') != 'en') {
1212
return strlen($value) <= $limit
1313
? $value
1414
: mb_substr($value, 0, $limit, 'UTF-8').$end;

0 commit comments

Comments
 (0)