Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search feature #78

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/PrettyRoutesController.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
<?php namespace PrettyRoutes;
<?php

use Route;
use Closure;
namespace PrettyRoutes;

class PrettyRoutesController {
use Closure;
use PrettyRoutes\Utils\RouteUtils;

class PrettyRoutesController
{
/**
* Show pretty routes.
*
* @return \Illuminate\Http\Response
*/
public function show()
{
$search = request('search');

$middlewareClosure = function ($middleware) {
return $middleware instanceof Closure ? 'Closure' : $middleware;
};

$routes = collect(Route::getRoutes());
if ($search) {
$routes = collect(RouteUtils::filter($search));
} else {
$routes = collect(RouteUtils::get());
}

foreach (config('pretty-routes.hide_matching') as $regex) {
$routes = $routes->filter(function ($value, $key) use ($regex) {
Expand All @@ -29,5 +37,4 @@ public function show()
'middlewareClosure' => $middlewareClosure,
]);
}

}
76 changes: 76 additions & 0 deletions src/Utils/RouteUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace PrettyRoutes\Utils;

use Illuminate\Support\Facades\Route;

class RouteUtils
{
/**
* Get all routes
*
* @return array
*/
public static function get()
{
return Route::getRoutes();
}

/**
* Filter Routes By Search Text
*
* @param string $search Search text
* @return array
*/
public static function filter($search)
{
$routes = [];
$search = strtolower($search);
foreach (Route::getRoutes() as $route) {
$next = false;
// methods filter
foreach (array_diff($route->methods(), config('pretty-routes.hide_methods')) as $method) {
if (str_contains(strtolower($method), $search)) {
array_push($routes, $route);
$next = true;
}
}
if ($next) continue;

// domain filter
if (strlen($route->domain()) != 0 and str_contains(strtolower($route->domain()), $search)) {
array_push($routes, $route);
continue;
}

// uri filter
if (str_contains(strtolower($route->uri()), $search)) {
array_push($routes, $route);
continue;
}

// name filter
if (str_contains(strtolower($route->getName()), $search)) {
array_push($routes, $route);
continue;
}

// action filter
if (str_contains(strtolower($route->getActionName()), $search)) {
array_push($routes, $route);
continue;
}

// middleware filter
foreach (array_diff($route->middleware(), config('pretty-routes.hide_methods')) as $middleware) {
if (str_contains(strtolower($middleware), $search)) {
array_push($routes, $route);
$next = true;
}
}
if ($next) continue;
}

return $routes;
}
}
18 changes: 17 additions & 1 deletion views/routes.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,28 @@
table.hide-domains .domain {
display: none;
}

.float-right {
float: right;
}

.clear-search {
color:black;
font-weight: bold;
}
</style>
</head>
<body>

<h1 class="display-4">Routes ({{ count($routes) }})</h1>


<form action="">
<input type="text" name="search" placeholder="Search" value="{{ request('search') != null ? request('search') : "" }}">
@if(request('search') != null)
<a class="clear-search" href="?search=">x</a>
@endif
</form>

<table class="table table-sm table-hover" style="visibility: hidden;">
<thead>
<tr>
Expand Down