Completed
Push — master ( 02f92d...9c9507 )
by Sebastian
15:38
created

RouteServiceProvider::mapFrontRoutes()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 12
nc 1
nop 1
1
<?php
2
3
namespace App\Providers;
4
5
use App\Models\Article;
6
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
7
use Illuminate\Routing\Router;
8
9
class RouteServiceProvider extends ServiceProvider
10
{
11
    protected $namespace = 'App\Http\Controllers';
12
13
    public function boot(Router $router)
14
    {
15
        $this->app->setLocale($this->app['currentLocale']->determine());
16
17
        $this->registerMacros($router);
18
19
        parent::boot($router);
20
    }
21
22
    public function map(Router $router)
23
    {
24
        $router->group(['namespace' => $this->namespace], function () {
25
            require app_path('Http/Routes/routes.php');
26
        });
27
    }
28
29
    protected function registerMacros(Router $router)
30
    {
31
        $router->macro('module', function ($slug, $className, $sortable = false) use ($router) {
32
            if ($sortable) {
33
                $router->patch("{$slug}/changeOrder", "{$className}Controller@changeOrder");
34
            }
35
36
            $router->resource($slug, "{$className}Controller");
37
        });
38
39
        $router->macro('articleList', function ($technicalNamePrefix, $action) use ($router) {
40
41
            $articles = Article::getWithTechnicalNameLike($technicalNamePrefix);
42
43
            $router->get(app()->getLocale().'/'.fragment_slug("navigation.{$technicalNamePrefix}"),  function () use ($articles) {
44
                return redirect(route("{$articles->first()->technical_name}"));
45
            })->name($technicalNamePrefix);
46
47
            $articles->map(function ($article) use ($technicalNamePrefix, $action, $router) {
48
                $router->get(app()->getLocale().'/'.fragment_slug("navigation.{$technicalNamePrefix}").'/'.$article->url, $action)->name("{$article->technical_name}");
49
            });
50
        });
51
52
        $this->app['paginateroute']->registerMacros();
53
    }
54
}
55