Forem

Hanane Kacemi
Hanane Kacemi

Posted on

2 1

100 days of code - Day 2

Today, we'll go through the Symfony Routing component, which allows to set up routing in PHP applications.

We can add this component to the project with this command : composer require symfony/routing

the main Classes of this component that we will see are : RouteCollection, Route, UrlMatcher,RequestContext

Let's create an instance of RouteCollection, we will use it to define all routes of our application :

$routes = new RouteCollection();

To add a route to this collection, we use the function "add" we give it a name and we create an instance of Route which is defined by a route pattern and an array of default values for route attributes, below are 2 examples :

$routes->add('greeting', new Route('/hello/{name}', ['name' => 'world']));
$routes->add('goodbye', new Route('/bye'));
Enter fullscreen mode Exit fullscreen mode

in order to match the current URL with one of those in our Collection, we need to use UrlMatcher :

$matcher = new UrlMatcher($routes, $context);

$context is an object of RequestContext, it holds the current request context informations :

$context = new RequestContext();
$context->fromRequest (Request::createFromGlobals());
Enter fullscreen mode Exit fullscreen mode

now We have everything we need to match any route against the predefined routes that we have ($routes object). To do that we use match method of $matcher object

$matcher->match($context->getPathInfo());

If the route is found, we get the custom attributes associated with that route. Otherwise, the ResourceNotFoundException exception is thrown if there's no route associated with the current URI.

Here is the output of $parameters if we access to /hello/joe :

var_dump($parameters)

 /* Result:
[
    'name' => 'joe',
    '_route' => 'hello',
];
*/
Enter fullscreen mode Exit fullscreen mode

That's all for today, see you next time.
Have a nice day

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay