Skip to content

Instantly share code, notes, and snippets.

@nunocodex
Created March 19, 2014 12:52
Show Gist options
  • Save nunocodex/9641019 to your computer and use it in GitHub Desktop.
Save nunocodex/9641019 to your computer and use it in GitHub Desktop.
Routing implementation with language support for cockpit/rapido
<?php
/* Route to content mapping for projects with languages feature (ex. /en/projects) */
$site->bind("/:lang/projects", function($params) use($site) {
/* Define view */
$view = false;
/* Define route */
$route = str_replace('../', '', rtrim($site["route"], '/'));
/* Get language param */
$lang = $params['lang'];
/* Filter route with lang param */
$route = str_replace('/' . $lang, '', $route);
/* Get all collection based on lang avariable */
$collection = collection('projects')->find([ 'lang' => $lang ])->toArray();
/* Get file from route */
$path = $site->path("content:" . (strlen($route) ? $route : '/'));
/* Prevent direct access to files in the content folder */
if ( $path && is_file($path) ) return false;
/* Show page not found if not found any collections valid */
if ( ! $collection ) return false;
/* Assign view */
$view = $site->path("content:{$route}.php");
/* Render page with relative params */
return $view ? $site->module("rapido")->render_page($view, array('collection' => $collection)) : false;
});
/* Route to content mapping for projects with language feature (ex. /en/project/dummy-project) */
$site->bind("/:lang/project/:slug", function($params) use($site) {
/* Define view */
$view = false;
/* Define route */
$route = str_replace('../', '', rtrim($site["route"], '/'));
/* Get slug from route */
$slug = $params['slug'];
/* Get lang from route */
$lang = $params['lang'];
/* Remove slug from route for found main file content */
$route = str_replace('/' . $slug, '', $route);
/* Remove lang from route */
$route = str_replace('/' . $lang, '', $route);
/* Get collection from slug */
$collection = collection('projects')->findOne([ 'lang' => $lang, 'slug' => $slug ]);
/* Get file from route */
$path = $site->path("content:" . (strlen($route) ? $route : '/'));
/* Prevent direct access to files in the content folder */
if ( $path && is_file($path) ) return false;
/* Show page not found if not found any collections valid */
if ( ! $collection ) return false;
/* Assign view */
$view = $site->path("content:{$route}.php");
/* Render page with relative params */
return $view ? $site->module("rapido")->render_page($view, array('collection' => $collection)) : false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment