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

Add named routes to routing component #90

Open
wants to merge 2 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
5 changes: 4 additions & 1 deletion components/routing/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
// Create the router instance
$router = new Router($events, $container);

// Create Url generator instance
$urlGenerator = new UrlGenerator($router->getRoutes(), $request);

// Load the routes
require_once 'routes.php';

// Create the redirect instance
$redirect = new Redirector(new UrlGenerator($router->getRoutes(), $request));
$redirect = new Redirector($urlGenerator);

// use redirect
// return $redirect->home();
Expand Down
19 changes: 17 additions & 2 deletions components/routing/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,25 @@
});

$router->group(['namespace' => 'App\Controllers', 'prefix' => 'users'], function (Router $router) {
$router->get('/', ['name' => 'users.index', 'uses' => 'UsersController@index']);
$router->post('/', ['name' => 'users.store', 'uses' => 'UsersController@store']);
$router->get('/', ['as' => 'users.index', 'uses' => 'UsersController@index']);
$router->post('/', ['as' => 'users.store', 'uses' => 'UsersController@store']);
});

// Naming routes using `as`:
$router->get('/articles', ['as' => 'articles.index', 'uses' => function () use ($urlGenerator) {
return 'The url to the route named "articles.index" is: ' . $urlGenerator->route('articles.index');
}]);

// Naming routes using `name` method:
// Note: You are responsible for refreshing name & action lookups if you named your
// routes using `name` method otherwise an exception of "route not defined" will be thrown.
$router->get('/articles/create', function () use ($urlGenerator) {
return 'The url to the route named "articles.create" is: ' . $urlGenerator->route('articles.create');
})->name('articles.create');

$router->getRoutes()->refreshNameLookups();
$router->getRoutes()->refreshActionLookups();

// catch-all route
$router->any('{any}', function () {
return 'four oh four';
Expand Down