Accessing URI Segments


Related Links

Introduction

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:

{% verbatim %}{{ request_segment(3) }}{% endverbatim %}

A more practical application might look something like this:

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

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

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

Route Parameters

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

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