Fork Phpillip on GitHub

Route Helpers

Phpillip's Route extends Silex's Route and provide an extra set of helpers.

These helpers are designed to save you time by addressing common controller needs automatically: parameters to content conversion, pagination, template resolution...

Content issues

When building a route that depends on the content (e.g. /blog/{article}), Phpillip will need to call the route for each article content you have: blog/my-first-post, blog/my-second-post...

That's why Phpillip provides helpers to link your routes to your contents.

Single content

To tell Phpillip that your route vary over content, use the content method on the route:

<?php 
// For an 'article' content type:
$app->get('/blog/{article}')->content('article');

In this example, Phpillip will call the route for each article file found in src/Resources/data/article.

The content helper also acts as a param converter.

When a Request hits a route that is declared as having a content, Phpillip will try to fetch the content for you.

In our previous example, calling the url /blog/my-first-post will match our route and Phpillip will look for a file named src/Resources/data/article/my-first-post.*.

If such file exists, Phpillip will parse it as an associative array and set it as an article attribute in the Request.

Of course, you can get the automatically fetched content in your controller:

<?php 
// As a parameter:
function (array $article) {
    // Your content is $article
}

// And in the Request attributes:
function (Request $request) {
    $article = $request->attributes->get('article');
}

Content list

To tell Phpillip that your route is a list of contents, use the contents methode on the route:

<?php 
// For an 'article' content type:
$app->get('/blog')->contents('article');

This only acts as a parameter converter.

When a Request hits a route that is declared as being a list −in our example /blog− Phpillip will look for all files in src/Resources/data/article. Then it will parse each file as an associative array, store the results in an array and set it as an articles attribute in the Request.

To get the automatically fetched contents in your controller:

<?php 
// As a parameter:
function (array $articles) {
    // Your articles are in $articles
}

// And in the Request attributes:
function (Request $request) {
    $articles = $request->attributes->get('articles');
}

By default, the contents are indexed numerically in the array and in the same order as they appear in the file system.

You can define custom index and sorting in the contents method: The index (string) is a key available in the content. The order (boolean) is true for ascending and false for descending.

To fetch every articles, indexed by date and most recent first (descending):

<?php 
$app->get('/blog')->contents('article', 'date', false);

Pagination

You need your contents paginated? The method paginate works just like the method contents but paginate your contents.

To tell Phpillip that your route is a paginated list of contents, use the contents method on the route:

<?php 
// For an 'article' content type:
$app->get('/blog')->paginate('article');

The paginate helper adds a page optional parameter to the route so that it can handle the following urls: /blog and /blog/{page}.

When building a route that paginates content, Phpillip will need to call the route for each page, depending on how much content you have: blog, blog/2, blog/3...

Note: the page parameters is ommitted on the first page.

The paginate helper then acts as a param converter.

When a Request hits a route that is declared as being a paginated list −in our example /blog/2− Phpillip will count files in src/Resources/data/article and parse the subset corresponding to the requested page.

The paginate helper defines 3 attributes in the Request:

Key Type Description
articles array The contents corresponding to the requested page.
page integer The requested page
paginator Phpillip\Model\Paginator The total number of pages in the pagination

Request attributes are available in the controller as always:

<?php 
// As parameters:
function (array $articles, Paginator $paginator, page) {
    // $articles contains only articles of the curent page
}

// And in the Request attributes:
function (Request $request) {
    $articles  = $request->attributes->get('articles');
    $paginator = $request->attributes->get('paginator');
    $page      = $request->attributes->get('page');
}

Hide a route

To hide a route from the build, use the hide method.

<?php 
$app
    ->get('_latest-posts')
    ->content('post')
    ->bind('latest_posts')
    ->hide()

This can be useful for a route that is meant to be rendered as a part of other routes, but should not generate a static file on its own.

Like a Twig render: {{ render(url('latest_posts')) }}

Route format

Route format is treated in its dedicated documentation.

Template

Template resolution is treated in its dedicated documentation.