Last active
August 29, 2015 13:57
-
-
Save nunocodex/9641384 to your computer and use it in GitHub Desktop.
Custom routing with module implementation with cockpit / rapido
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Load custom helpers */ | |
require_once(__DIR__ . '/helpers.php'); | |
/* Load custom config */ | |
//$config = include(__DIR__ . '/config.php'); | |
/* Load custom route */ | |
require_once(__DIR__ . '/route.php'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
'lang' => [ | |
'enable' => true, | |
'valid' => ['en', 'it', 'de'], | |
'default' => 'en' | |
] | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Dump on file. | |
* | |
* @param array Variable to dump. | |
*/ | |
if ( ! function_exists('dump') ) { | |
function dump($var) { | |
echo "<pre>" . print_r($var, true) . "</pre>"; | |
} | |
} | |
/** | |
* Dump on error_log. | |
* | |
* @param array Variable to dump. | |
*/ | |
if ( ! function_exists('dump_log') ) { | |
function dump_log($var) { | |
error_log(print_r($var, true)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Define site object */ | |
$site = $this; | |
/* Get config (I need to found a valid way for this) */ | |
$config = include(__DIR__ . '/config.php'); | |
/* Route to content mapping for projects (ex. /projects) */ | |
$site->bind("/projects", function() use($site) { | |
/* Get config (I need to found a valid way for this) */ | |
$config = include(__DIR__ . '/config.php'); | |
/* Define view */ | |
$view = false; | |
/* Define route */ | |
$route = str_replace('../', '', rtrim($site["route"], '/')); | |
/* Validate language config if enabled, in this case ignore this redirecting to the valid route */ | |
if ( $config['lang']['enable'] ) { | |
$site->reroute("/{$config['lang']['default']}/projects"); | |
} | |
/* 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) { | |
/* Get config (I need to found a valid way for this) */ | |
$config = include(__DIR__ . '/config.php'); | |
/* 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); | |
/* Validate language config if enabled, in this case ignore this redirecting to the valid route */ | |
if ( $config['lang']['enable'] ) { | |
$site->reroute("/{$config['lang']['default']}/project/{$slug}"); | |
} | |
/* 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; | |
}); | |
/* If lang is enabled, extend the routing */ | |
if ( $config['lang']['enable'] ) { | |
/* 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