Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Router Laravel Package

joomla/router

Joomla Framework Router registers application routes and dispatches incoming request URIs to controller methods. PHP 8.1+ compatible, install via Composer (joomla/router ~3.0). Suitable for building clean, maintainable routing in PHP apps.

View on GitHub
Deep Wiki
Context7

Overview

The Router package is used to register an application's routes and to route the request URI to a controller method.

Adding maps

The purpose of a router is to find a controller based on a routing path. The path could be a URL for a web site, or it could be an end-point for a RESTful web-services API.

The addRoute method is used to map at routing pattern to a controller.

use Joomla\Router\Router;

$router = new Router;
$router
    ->addRoute('GET', '/article/:article_id', 'Acme\\ArticleController') // Route to Controller
	->addRoute('GET', '/component/*', function() { return true; }) // Route to Closure
	->addRoute('GET', '/user/:id', 'get_user'); // Route to function / callable

function get_user($id)
{
    // do stuff
}

By default, named variables in the defined route have a matching regex of ([^/]*), which matches everything in a route, up to the next /. You can optionally define the exact regex to use, by passing a third parameter to the addMaps function. This is an associative array using the named variables as the keys and the desired regex as the values.

use Joomla\Router\Router;

$router = new Router;
$router->addRoute(
    'GET',
    '/user/:id',
    'UserController@show',
    array(
        'id' => '(\d+)'
    )
);

If you were to take the above code, and match it against a path of /user/123, you would receive the following array back.

$match = $router->parseRoute('/user/123');

print_r($match);
// Array
// (
//     [controller] => UserController@show
//     [vars] => Array
//         (
//             [name] => user
//         )
// )

At this point, you can do whatever you want with the returned data. It's up to you to instantiate your controller, or just use call_user_func_array($matched['controller'], $matched['vars']) in your FrontController if you're using the Router as the backbone for a micro framework. It's completely flexible.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport