/** * Execute the console command. * * @return mixed */ public function fire() { // Prepare input $namespace = $this->getNamespace(); $path = $this->getPath(); // Prepare schema fields if (!$this->option('no-migration')) { $this->generateSchemaMigration(); } // 2. Generate model $this->call('apigen:model', ['name' => $this->translator->translate($this->argument('name'))->toModelName(), '--path' => $path, '--namespace' => $namespace]); // 3. Generate repository $this->call('apigen:repository', ['name' => $this->translator->translate($this->argument('name'))->toModelName(), '--path' => $path, '--namespace' => $namespace]); // 4. Generate controller if (!$this->option('no-controller')) { $this->generateController(); } // 5. Setup API route if (!$this->option('no-route') && !$this->option('no-controller')) { $resourceName = $this->translator->translate($this->argument('name'))->toTableName(); $repositoryName = $this->translator->translate($this->argument('name'))->toRepositoryName(); $routeBaseName = "api.{$resourceName}"; if ($this->router->getRoutes()->hasNamedRoute("{$routeBaseName}.index")) { $this->error("Assuming routes for {$this->translator->translate($this->argument('name'))->toReadableName()} already exists."); } else { $this->info("Setting up route for '{$repositoryName}' ..."); $routeString = "\n\nRoute::resource('{$resourceName}', '{$namespace}\\{$repositoryName}\\{$repositoryName}Controller', [ 'names' => [ " . "\n 'index' => '{$routeBaseName}.index', " . "\n 'create' => '{$routeBaseName}.create', " . "\n 'store' => '{$routeBaseName}.store', " . "\n 'show' => '{$routeBaseName}.show', " . "\n 'edit' => '{$routeBaseName}.edit', " . "\n 'update' => '{$routeBaseName}.update', " . "\n 'destroy' => '{$routeBaseName}.destroy', " . "\n] ]);"; $this->filesystem->append(app_path() . '/routes.php', $routeString); } } // 6. Setup administration interface if needed if (!$this->option('no-admin')) { $this->setupAdministrationBackend(); } }
/** * @return RouteCollection */ public static function getRoutes() { if (is_null(self::$routes)) { self::$routes = self::$router->getRoutes(); } return self::$routes; }
/** * Get route list protected by checkPermission middleware * * @return array */ public function getProtectedRouteList() { if (is_null($this->protectedRouteList)) { $this->protectedRouteList = []; $routeList = $this->router->getRoutes(); /** @var \Illuminate\Routing\Route $route */ foreach ($routeList as $route) { // we need only routes which has permission checking middleware if (in_array('checkPermission', $route->middleware()) === false) { continue; } if ($route->getActionName() == 'Closure') { // permissions and closures doesn't work together continue; } $routeName = $route->getName(); if (empty($routeName)) { // route name should not be empty continue; } $this->protectedRouteList[$routeName] = ['route_action_name' => $route->getActionName(), 'route_name' => $routeName, 'controller_name' => $this->extractControllerName($route->getActionName()), 'controller_action_name' => $this->extractControllerActionName($route->getActionName())]; } } return $this->protectedRouteList; }
/** * Compile the routes into a displayable format. * * @return array */ public function getRoutes() { $results = []; foreach ($this->router->getRoutes() as $route) { $results[] = $this->getRouteInformation($route); } return array_filter($results); }
/** * Merge the existing routes with the new routes. * * @param \Illuminate\Routing\RouteCollection $routes * * @return \Illuminate\Routing\RouteCollection */ protected function mergeExistingRoutes(RouteCollection $routes) { if (!isset($this->oldRoutes)) { $this->oldRoutes = $this->router->getRoutes(); } foreach ($this->oldRoutes as $route) { $routes->add($route); } return $routes; }
/** * {@inheritdoc} */ public function getForPath($path) { /** @var Request $request */ $request = Request::create($path); try { $collectionMatcher = new RouteCollectionMatcher($this->router->getRoutes()); if ($route = $collectionMatcher->getRouteForRequest($request)) { return $this->extractPermissionFrom($route); } } catch (HttpException $e) { } return null; }
/** * @return mixed get the data to be serialized */ public function getData() { $this->router = app('router'); $this->url = app('url'); $data = array('currentRoute' => $this->router->current()); $routes = $this->router->getRoutes(); $results = array(); foreach ($routes as $name => $route) { $results[] = $this->getRouteInformation($route, $data['currentRoute']); } $data['routes'] = $results; return array('router' => $data); }
/** * Merge the old application routes with the API routes. * * @param string $version * * @return array */ protected function mergeOldRoutes($version) { if (!isset($this->oldRoutes)) { $this->oldRoutes = $this->router->getRoutes(); } if (!isset($this->mergedRoutes[$version])) { $this->mergedRoutes[$version] = $this->routes[$version]; foreach ($this->oldRoutes as $route) { $this->mergedRoutes[$version]->add($route); } } return $this->mergedRoutes[$version]; }
protected function excludedRouteNames($request) { $routes = $this->router->getRoutes(); foreach ($this->excludedRouteNames as $name) { if ($routes->hasNamedRoute($name)) { $route = $routes->getByName($name); if ($route->matches($request)) { return true; } } } return false; }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $route = $this->router->getRoutes()->match($request); if ($route) { $subdomain = $route->parameter(config('tenant.subdomain')); $config = $this->tenantManager->getDatabaseConfig($subdomain); if ($config) { config()->set("database.connections.tenant", $config); $this->db->setDefaultConnection('tenant'); $this->db->reconnect('tenant'); return $next($request); } } abort(404); }
/** * @return string */ public function last_page() { $lang = null; $collection = $this->router->getRoutes(); $route = $collection->match(Request::create($this->wrappedObject->last_page)); if ($route->getName() != null) { $langOptions = $this->getWioData($route->getName(), $route->parameters()); if (!isset($langOptions['url'])) { $langOptions['url'] = route($route->getName(), $route->parameters()); } if (!isset($langOptions['langString'])) { $langString = 'online.' . $route->getName(); } else { $langString = 'online.' . $langOptions['langString']; unset($langOptions['langString']); } $lang = $this->translator->get($langString, $langOptions); // May happen if we have two routes 'xy.yx.zz' and 'xy.yx' if (is_array($lang)) { $lang = $this->translator->get($langString . '.index', $langOptions); } } if ($lang == null) { // $lang = Lang::get('online.unknown', ['url' => '']); // Used for debugging, should be left here until we have added all routes $lang = 'online.' . $route->getName(); } return $lang; }
/** * Create a new route command instance. * * @param \Illuminate\Routing\Router $router * @param \Illuminate\Contracts\Config\Repository $config * @param \Illuminate\Filesystem\Filesystem $filesystem */ public function __construct(Router $router, ConfigRepository $config, Filesystem $filesystem) { parent::__construct(); $this->routes = $router->getRoutes(); $this->config = $config->get('path2api'); $this->filesystem = $filesystem; }
public function testResourceRoute() { /* @var Dispatcher $dispatcher */ $dispatcher = $this->getMock(Dispatcher::class); $router = new Router($dispatcher, null); $routeHelper = new RouteHelper($router); $routeHelper->resource('test', 'TestController')->get('get_test', 'get_test')->post('post_test', 'post_test')->put('put_test', 'put_test')->patch('patch_test', 'patch_test')->delete('delete_test', 'delete_test')->rawGet('rawget', 'rawget')->rawPost('rawpost', 'rawpost')->rawPut('rawput', 'rawput')->rawPatch('rawpatch', 'rawpatch')->rawDelete('rawdelete', 'rawdelete')->done(); $routes = $router->getRoutes(); $this->assertRoute($routes, 'test', 'GET', 'TestController@index'); $this->assertRoute($routes, 'test?' . RouteHelper::PAGINATION_URI, 'GET', 'TestController@index'); $this->assertRoute($routes, 'test', 'POST', 'TestController@store'); $this->assertRoute($routes, 'test/{test}', 'GET', 'TestController@show'); $this->assertRoute($routes, 'test/{test}', 'PUT', 'TestController@update'); $this->assertRoute($routes, 'test/{test}', 'PATCH', 'TestController@update'); $this->assertRoute($routes, 'test/{test}', 'DELETE', 'TestController@destroy'); $this->assertRoute($routes, 'test/{test}/get_test', 'GET', 'TestController@get_test'); $this->assertRoute($routes, 'test/{test}/post_test', 'POST', 'TestController@post_test'); $this->assertRoute($routes, 'test/{test}/put_test', 'PUT', 'TestController@put_test'); $this->assertRoute($routes, 'test/{test}/patch_test', 'PATCH', 'TestController@patch_test'); $this->assertRoute($routes, 'test/{test}/delete_test', 'DELETE', 'TestController@delete_test'); $this->assertRoute($routes, 'test/rawget', 'GET', 'TestController@rawget'); $this->assertRoute($routes, 'test/rawpost', 'POST', 'TestController@rawpost'); $this->assertRoute($routes, 'test/rawput', 'PUT', 'TestController@rawput'); $this->assertRoute($routes, 'test/rawpatch', 'PATCH', 'TestController@rawpatch'); $this->assertRoute($routes, 'test/rawdelete', 'DELETE', 'TestController@rawdelete'); }
public function __construct(Arr $arr, Request $request, Router $router) { $this->arr = $arr; $this->request = $request; $this->user = $request->user(); $this->router = $router; $this->routes = $router->getRoutes(); }
public function __construct(Router $router, Filesystem $filesystem, $appUrl, $savePath, $saveName) { parent::__construct(); $this->routes = $router->getRoutes(); $this->filesystem = $filesystem; $this->appUrl = $appUrl; $this->filePath = $savePath . '/' . $saveName . '.html'; }
public function __construct(RouteCollection $collection, Router $router) { $this->routes = $collection; foreach ($router->getRoutes() as $route) { $routeInfo = (new RouteInfo($route, ['router' => 'Laravel']))->toArray(); $this->routes->push($routeInfo); } }
/** * Retreives the permissions to be registered. * * @return array */ public function permissionsToRegister() { $config = config('acl'); $permissionPlaceholders = array_get($config, 'permission_placeholders'); $additionalPermissions = array_get($config, 'additional'); $permissionsToRegister = []; $routes = array_filter($this->router->getRoutes()->getRoutes(), function ($route) { return $route->getName() and !in_array($route->getName(), AclPolicy::getExcept()); }); foreach ($routes as $route) { if (array_key_exists($route->getName(), $permissionPlaceholders)) { $permissionsToRegister[$route->getName()] = $permissionPlaceholders[$route->getName()]; } else { $permissionsToRegister[$route->getName()] = $route->getName(); } } return array_merge($permissionsToRegister, $additionalPermissions); }
/** * @param string $domain * * @return RouteCollection */ protected function resolveTenantRoutes($domain) { if (null === ($dt = $this->repository->findOneByDomain($domain))) { return $this->error(sprintf('No tenant found for "%s", are you sure you entered the correct domain?', $domain)); } /** @var Tenant $tenant */ $tenant = app('auth.tenant'); $tenant->updateTenancy(new NullUser(), $dt->getTenantOwner(), $dt); $this->resolver->boot($this->router); return $this->routes = $this->router->getRoutes(); }
/** * Bootstrap any application services. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { $this->setRootControllerNamespace(); if ($this->app->routesAreCached()) { $this->loadCachedRoutes(); } else { $this->loadRoutes(); $this->app->booted(function () use($router) { $router->getRoutes()->refreshNameLookups(); }); } }
/** * Merge routes defined by packages into API router. * * @param \Dingo\Api\Routing\Router $api * @param \Illuminate\Routing\Router $router * @return void */ protected function mergePackgeRoutes(ApiRouter &$api, Router $router) { foreach ($router->getRoutes() as $route) { $api->version(array_keys($api->getRoutes()), function ($api) use($route) { $action = $route->getAction(); // Remove prefix if present if (isset($action['prefix'])) { unset($action['prefix']); } $api->addRoute($route->getMethods(), $route->uri(), $action); }); } }
/** * Execute the console command. * * @return mixed */ public function fire() { $this->call('route:clear'); $routes = $this->router->getRoutes(); if (count($routes) == 0) { return $this->error("Your application doesn't have any routes."); } foreach ($routes as $k => &$route) { if (in_array($route->getUri(), $this->config['ignoreUris'])) { unset($k); } else { $route->prepareForSerialization(); try { serialize($route); } catch (\Exception $e) { throw new \LogicException("\n Unable to prepare route [{$route->getUri()}] for serialization.\n \nPlace it in the ignoreUris config or make it not a closure.\n "); } } } $this->files->put($this->config['compiledPath'], $this->buildRouteCacheFile($routes)); $this->info('Routes cached successfully!'); }
/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $route = $this->route->getRoutes()->match($request); $ignore = false; if ($route) { foreach ($this->routeNameIgnore as $ignoreName) { if (preg_match("/{$ignoreName}/", $route->getName())) { $ignore = true; break; } } foreach ($this->routePathIgnore as $ignorePath) { if (preg_match("/{$ignorePath}/", $route->getPath())) { $ignore = true; break; } } } if ($ignore) { return $next($request); } return parent::handle($request, $next); }
/** * Define the routes for the application. * * @param \Illuminate\Routing\Router $router * @return void */ public function map(Router $router) { $router->group(['namespace' => $this->namespace], function ($router) { require app_path('Http/routes.php'); /** * This is a hack so that Presenters and Webservices route will work */ $default = 'App\\Http\\Controllers'; $namespaces = ['App\\Http\\Presenters', 'App\\Http\\WebServices']; $routes = $router->getRoutes(); foreach ($routes as $index => $route) { $namespace = explode('@', $route->getActionName())[0]; if (!class_exists($namespace)) { foreach ($namespaces as $val) { $newNamespace = str_replace($default, $val, $namespace); if (class_exists($newNamespace)) { $action = str_replace($default, $val, $route->getAction()); $route->setAction($action); } } } } }); }
/** * Returns a given route's children as an array. A parent route like /users would return (if they existed) all routes * like /users/{userid}, /users/new. * * @param Route $parentRoute * @return Route[] */ public function subordinates(Route $parentRoute) { if (array_key_exists($parentRoute->getUri(), $this->subordinateRouteCache)) { return $this->subordinateRouteCache[$parentRoute->getUri()]; } $routes = $this->router->getRoutes(); $children = []; /** @var Route $route */ foreach ($routes as $route) { if (!self::isValid($route)) { continue; } // if the route does not start with the same uri as the current route -> skip if ($parentRoute->getUri() != '/' && !starts_with($route->getUri(), $parentRoute->getUri())) { continue; } // if route equals the parent route if ($parentRoute->getActionName() == $route->getActionName()) { continue; } $children[] = $route; } return $this->subordinateRouteCache[$parentRoute->getUri()] = $children; }
/** * Get the underlying route collection. * * @return \Illuminate\Routing\RouteCollection * @static */ public static function getRoutes() { return \Illuminate\Routing\Router::getRoutes(); }
/** * Render routes table. * * @param Router $router * * @return Table */ protected function renderRoutes(Router $router) { return new Table(['class' => 'table'], [new TableHeader([], new TableRow([], [new TableHeaderCell([], 'URI'), new TableHeaderCell([], 'Action')])), new TableBody([], Std::map(function (Route $route) { return new TableRow([], [new TableCell([], $route->getUri()), new TableCell([], new PreformattedText(['class' => 'pre-scrollable'], $route->getActionName()))]); }, $router->getRoutes()))]); }
/** * Create a new route command instance. * * @param \Illuminate\Routing\Router $router * @return void */ public function __construct(Router $router) { parent::__construct(); $this->router = $router; $this->routes = $router->getRoutes(); }
/** * Create a new routes instance instance. * * @param \Illuminate\Routing\Router $router * @return void */ public function __construct(Router $router, Request $request) { $this->router = $router; $this->request = $request; $this->routes = $router->getRoutes(); }
/** * Get the route collection. * * @return \Illuminate\Routing\RouteCollection */ public function getRoutes() { return $this->router->getRoutes(); }
/** * Extract attributes for current url * * @param string|null|false $url to extract attributes, if not present, the system will look for attributes in the current call * * @return array Array with attributes * */ protected function extractAttributes($url = false) { if (!empty($url)) { $attributes = []; $parse = parse_url($url); if (isset($parse['path'])) { $parse = explode("/", $parse['path']); } else { $parse = []; } $url = []; foreach ($parse as $segment) { if (!empty($segment)) { $url[] = $segment; } } foreach ($this->router->getRoutes() as $route) { $path = $route->getUri(); if (!preg_match("/{[\\w]+}/", $path)) { continue; } $path = explode("/", $path); $i = 0; $match = true; foreach ($path as $j => $segment) { if (isset($url[$i])) { if ($segment === $url[$i]) { $i++; continue; } if (preg_match("/{[\\w]+}/", $segment)) { // must-have parameters $attribute_name = preg_replace(["/}/", "/{/", "/\\?/"], "", $segment); $attributes[$attribute_name] = $url[$i]; $i++; continue; } if (preg_match("/{[\\w]+\\?}/", $segment)) { // optional parameters if (!isset($path[$j + 1]) || $path[$j + 1] !== $url[$i]) { // optional parameter taken $attribute_name = preg_replace(["/}/", "/{/", "/\\?/"], "", $segment); $attributes[$attribute_name] = $url[$i]; $i++; continue; } } } else { if (!preg_match("/{[\\w]+\\?}/", $segment)) { // no optional parameters but no more $url given // this route does not match the url $match = false; break; } } } if (isset($url[$i + 1])) { $match = false; } if ($match) { return $attributes; } } } else { if (!$this->router->current()) { return []; } $attributes = $this->router->current()->parameters(); $response = event('routes.translation', [$attributes]); if (!empty($response)) { $response = array_shift($response); } if (is_array($response)) { $attributes = array_merge($attributes, $response); } } return $attributes; }