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

Commit 0f512e5

Browse files
committed
- add desc for page
- check for Route::currentRouteName() internally for navigation - make params avail to controller through using cache - make static (closure) routes cacheable through using dummy controller rdme Update README.md
1 parent a91e559 commit 0f512e5

File tree

8 files changed

+79
-15
lines changed

8 files changed

+79
-15
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ return [
3939
'a' => 'is-active',
4040
],
4141

42+
/*
43+
* where to search for the template views relative to "resources\views" folder
44+
*/
45+
'templatePath' => 'pages',
46+
4247
/*
4348
* the path where we will save the route list for multiLocal route resolving
4449
*/
@@ -69,6 +74,19 @@ return [
6974
});
7075
```
7176

77+
### Good Practice
78+
Ofcourse you are free to code your app the way you want, but just in-case here are the naming convention the package use.
79+
80+
- `title` > `title_case(some title)`
81+
- `route_name` > `str_slug(title)`
82+
- `action` > `SomeController\camelCase(title)`
83+
84+
## Notes
85+
- for the pages with "action" you can get all the page params like **template, title, body, desc, breadCrump** inside your "action@method" by using `extract(cache('the_route_name'));`
86+
87+
<u>**or**</u>
88+
- if you followed the naming convention above, you can instead use `extract(cache(kebab_case('TheCurrentMethodName')));`
89+
7290
# ToDo
7391

7492
* [ ] CRUD Views for (roles/perms/pages/menus). *any help is appreciated*

src/Controller/DummyController.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace ctf0\SimpleMenu\Controller;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Support\Facades\Route;
7+
8+
class DummyController extends Controller
9+
{
10+
public function handle()
11+
{
12+
extract(cache(Route::currentRouteName()));
13+
14+
return view(config('simpleMenu.templatePath').".$template")->with([
15+
'title' => $title,
16+
'body' => $body,
17+
'desc' => $desc,
18+
'breadCrump' => $breadCrump,
19+
]);
20+
}
21+
}

src/Models/Page.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Page extends Node
1414
use HasRoles, HasTranslations;
1515

1616
protected $guarded = ['id'];
17-
public $translatable = ['title', 'body', 'prefix', 'url'];
17+
public $translatable = ['title', 'body', 'desc', 'prefix', 'url'];
1818

1919
public static function boot()
2020
{
@@ -32,6 +32,7 @@ public static function boot()
3232
});
3333
}
3434

35+
Cache::forget($model->route_name);
3536
File::delete(config('simpleMenu.routeListPath'));
3637
});
3738

@@ -43,6 +44,7 @@ public static function boot()
4344
});
4445
}
4546

47+
Cache::forget($model->route_name);
4648
File::delete(config('simpleMenu.routeListPath'));
4749
});
4850
}

src/Traits/NavigationTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ctf0\SimpleMenu\Traits;
44

5+
use Illuminate\Support\Facades\Route;
56
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
67

78
trait NavigationTrait
@@ -14,8 +15,10 @@ trait NavigationTrait
1415
*
1516
* @return [type] [description]
1617
*/
17-
public function getUrl($name, $code)
18+
public function getUrl($code)
1819
{
20+
$name = Route::currentRouteName();
21+
1922
// routeName is not saved in the db (ex.php artisan make:auth)
2023
// or only url
2124
$routesListFile = include $this->listFileDir;

src/Traits/RoutesTrait.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ctf0\SimpleMenu\Traits;
44

55
use ctf0\SimpleMenu\Models\Page;
6+
use Illuminate\Support\Facades\Cache;
67
use Illuminate\Support\Facades\File;
78
use Illuminate\Support\Facades\Route;
89
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
@@ -19,7 +20,6 @@ public function createRoutes()
1920
{
2021
Route::group([
2122
'prefix' => LaravelLocalization::setLocale(),
22-
'namespace' => config('simpleMenu.pagesControllerNS'),
2323
'middleware' => [
2424
'web',
2525
LocaleSessionRedirect::class,
@@ -62,7 +62,7 @@ protected function pageComp($page)
6262
// page data
6363
$title = $page->title;
6464
$body = $page->body;
65-
$desc = $body;
65+
$desc = $page->desc;
6666
$template = $page->template;
6767
$breadCrump = $page->getAncestors();
6868

@@ -95,18 +95,19 @@ protected function routeGen($routeName, $url, $prefix, $action, $roles, $permiss
9595

9696
// dynamic
9797
if ($action) {
98-
Route::get($route, $action)->name($routeName)->middleware([$roles, $permissions]);
98+
Cache::forever($routeName, compact('template', 'title', 'body', 'desc', 'breadCrump'));
99+
100+
Route::get($route)
101+
->uses(config('simpleMenu.pagesControllerNS').'\\'.$action)
102+
->name($routeName)
103+
->middleware([$roles, $permissions]);
99104
}
100105
// static
101106
else {
102-
Route::get($route, function () use ($template, $title, $body, $desc, $breadCrump) {
103-
return view("pages.{$template}")->with([
104-
'title' => $title,
105-
'body' => $body,
106-
'desc' => $desc,
107-
'breadCrump' => $breadCrump,
108-
]);
109-
})
107+
Cache::forever($routeName, compact('template', 'title', 'body', 'desc', 'breadCrump'));
108+
109+
Route::get($route)
110+
->uses('\ctf0\SimpleMenu\Controller\DummyController@handle')
110111
->name($routeName)
111112
->middleware([$roles, $permissions]);
112113
}

src/config/simpleMenu.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
'a' => 'is-active',
1111
],
1212

13+
/*
14+
* where to search for the template views relative to "resources\views" folder
15+
*/
16+
'templatePath' => 'pages',
17+
1318
/*
1419
* the path where we will save the route list for multiLocal route resolving
1520
*/

src/database/migrations/2017_02_28_202951_create_pages_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function up()
2323
$table->string('route_name')->nullable();
2424
$table->json('title');
2525
$table->json('body')->nullable();
26+
$table->json('desc')->nullable();
2627
$table->json('prefix')->nullable();
2728
$table->json('url')->nullable();
2829
$table->timestamps();

src/database/seeds/PagesTableSeeder.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class PagesTableSeeder extends Seeder
1111
*/
1212
public function run()
1313
{
14-
$heros = ['Home', 'Contact Us', 'About'];
14+
$faker = Factory::create();
15+
16+
$heros = ['Home', 'About', 'Contact Us'];
1517
foreach ($heros as $one) {
1618
Page::create([
1719
'route_name'=> str_slug($one),
@@ -21,14 +23,21 @@ public function run()
2123
'en' => $one,
2224
'fr' => $one,
2325
],
26+
'body' => [
27+
'en' => $faker->text(),
28+
'fr' => $faker->text(),
29+
],
30+
'desc' => [
31+
'en' => $faker->text(),
32+
'fr' => $faker->text(),
33+
],
2434
'url' => [
2535
'en' => str_slug($one),
2636
'fr' => str_slug($one),
2737
],
2838
]);
2939
}
3040

31-
$faker = Factory::create();
3241
$i = 1;
3342
while ($i <= 20) {
3443
$en = $faker->unique()->city;
@@ -45,6 +54,10 @@ public function run()
4554
'en' => $faker->text(),
4655
'fr' => $faker->text(),
4756
],
57+
'desc' => [
58+
'en' => $faker->text(),
59+
'fr' => $faker->text(),
60+
],
4861
'prefix' => [
4962
'en' => str_slug($faker->unique()->country),
5063
'fr' => str_slug($faker->unique()->country),

0 commit comments

Comments
 (0)