Skip to content

Instantly share code, notes, and snippets.

@nunocodex
Last active June 17, 2019 14:10
Show Gist options
  • Save nunocodex/9640281 to your computer and use it in GitHub Desktop.
Save nunocodex/9640281 to your computer and use it in GitHub Desktop.
Projects and Project implementation route for Cockpit and Rapido.
<?php
/* Route to content mapping for projects (ex. /projects) */
$site->bind("/projects", function() use($site) {
/* Define view */
$view = false;
/* Define route */
$route = str_replace('../', '', rtrim($site["route"], '/'));
/* Get all collection */
$collection = collection('projects')->find()->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 (ex. /project/dummy-project) */
$site->bind("/project/:slug", function($params) use($site) {
/* Define view */
$view = false;
/* Define route */
$route = str_replace('../', '', rtrim($site["route"], '/'));
/* Grab slug from route */
$slug = $params['slug'];
/* Remove slug from route for found main file content */
$route = str_replace('/' . $slug, '', $route);
/* Get collection from slug */
$collection = collection('projects')->findOne([ '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