Accessing URI Segments

Related Links


In popular Laravel fashion you may want to leverage request segments directly in your view. This article will show you how to do just that.

Request Segments

Laravel's request object is mapped to Twig functions like request_*. So you can access the segment for example like this:

{{ request_segment(3) }}

A more practical application might look something like this:

{% include "theme::segments/" ~ request_segment(1) %}

In the below example we'll use the last segment to find an entry by it's ID:

{% set example = entries('example', 'widgets').find(request_segments()|last) %}

Route Parameters

In a similar fashion Laravel's route object is also mapped to route_* functions. With this you can easily grab named parameters:

{% include "theme::segments/" ~ route_parameter("segment") %}