Skip to content

Commit 9c2a8c1

Browse files
committed
add view variable that holds all route info as a css class
1 parent e1d1555 commit 9c2a8c1

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/ServiceProvider.php

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

33
namespace Zschuessler\RouteToClass;
44

5+
56
use Illuminate\Support\Facades\View;
67

78
class ServiceProvider extends \Illuminate\Support\ServiceProvider
@@ -13,7 +14,22 @@ class ServiceProvider extends \Illuminate\Support\ServiceProvider
1314
*/
1415
public function boot()
1516
{
16-
View::composer('*', function ($view) {
17+
View::composer('*', function (\Illuminate\View\View $view) {
18+
$route = request()->route()->getPath();
19+
20+
// Remove route parameters. product_id is removed here: `controller/product/{product_id}`
21+
$clean = preg_replace("/\{([^}]+)\}/", '', $route);
22+
23+
// Remove characters that aren't alpha, numeric, or dashes
24+
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
25+
26+
// Remove any double dashes from replace functions. eg: `product--name` should be `product-name`
27+
$clean = strtolower(trim($clean, '-'));
28+
29+
// Replace special characters with a dash
30+
$clean = preg_replace("/[\/_|+ -]+/", '-', $clean);
31+
32+
View::share('route_body_classes', $clean);
1733

1834
});
1935
}

0 commit comments

Comments
 (0)