What is reverse routing? Why do we need reverse routing? (Laravel)-detail explained.

Nisma Hossain
3 min readMar 28, 2024

--

Reverse routing in Laravel is a technique where instead of hardcoding URLs directly into your code, you give each route a unique name and then generate URLs dynamically based on these route names. This approach enhances flexibility, readability, and maintainability by allowing you to update route paths centrally and avoid hardcoded URLs throughout your application.

Example:

Imagine you’re giving directions to a place:

Normal Routing:

You tell someone the exact directions, like → “Turn left at the third street, then right at the park.

If the streets change or the park moves, you have to update every set of directions you’ve given.

Now, imagine you’re giving directions using landmarks (like street names):

Reverse Routing:

You tell someone, — -> “Just follow Maple Street until you reach Elm Street.

If the streets change, you only need to update the landmarks (like “Follow Oak Street until you reach Elm Street”).

Comparison:

Normal routing is like giving direct instructions (hardcoding URLs into your code).

Reverse routing is like giving instructions based on landmarks (using route names instead of hardcoded URLs).

Code Example:

Let’s say we have a simple Laravel application with a user profile page. We want to create a link to this profile page.

Step by step for both cases (normal route and reverse route):

1. Define a route for the user profile page in “routes/web.php”

For normal route:

For the reverse route:

2. Usage in View/Controller

For normal route: (directly using the URL)

For reverse route:(using with route function not directly using the URL)

Explanation:

Normal Routing:

  • We directly define the URL /profile in the route definition.
  • In our view or controller, we use this URL directly in the anchor tag (<a href=”/profile”>Profile</a>).

Reverse Routing:

  • We give the route a name (profile) using the name() method in the route definition.
  • In our view or controller, we use the route() helper function to generate the URL for the named route (<a href=”{{ route(‘profile’) }}”>Profile</a>).

Comparison:

  • Flexibility:

With reverse routing, if the URL for the user profile page changes, we only need to update it in one place (the route definition), rather than updating it everywhere it’s used in our code.

  • Readability:

Reverse routing makes our code more readable because we see route(‘profile’), which indicates what the URL is for, rather than a hardcoded URL like /profile.

  • Maintainability:

Reverse routing reduces the chance of errors and makes our code easier to maintain because we’re not hardcoding URLs everywhere. This can save time and effort when making changes to routes or URLs in the future.

In summary, reverse routing provides flexibility, readability, and maintainability benefits over normal routing, making it a preferred approach in Laravel applications.

--

--