2020

Simple blog manager

A small and very-much-in-progress PHP blog app that I've been working on intermittently. It runs this site! It has basic support for blog posts, and handles the management/display of this project list. I created it as a way to learn more about 'fancy' URL routing and because I couldn't find a small blog platform that wasn't very opinionated (and I'd rather deal with my code's opinions than others').

Here is a screenshot of the very, very Bootstrap-py posts list page:

Light blog posts page

And for you Inception fans (me being the biggest), here it is handling this post:

Inception, in light blog... BWAAAAMP.

I will update this post with more code samples along the way; for now, here is the snippet that handles parsing the request URL (which is redirected straight to index.php by htaccess for any request, with some exceptions for assets):

// get url parts
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// check if top level path was set before (catch login redirect)
if(!isset($route[0])){
$route = array_values(array_filter(explode('/',$path), function($v){
return $v != '' && $v != 'light-blog';
}));
}

And what just blew my mind is that I copied that code from PHPStorm, and TinyMCE kept the formatting! If you don't mind a bit of style=""'d spans messying your HTML, you can save some bloat and not install some code formatting library/plugin.

Anyway, about the code: it uses PHP's built-in parse_url() function to parse the URL to just the relevant trailing portion. Then we skip if a $route entry has already been set, meaning (at least for now) that a login page redirect has been set from earlier in the script. If not set, we digest the parsed URL into $route using a trifecta of PHP array functions. This array will contain ordered entries of the URL components, e.g. ['admin', 'posts', 'new'] which are then passed to the controller to retrieve the view options. Speaking of...

Let's give that code formatting another whirl. Here is the code that returns the view options for the 404 page:

public function error_404(){
return array(
'meta' => array(
"page_name" => '404',
"page_slug" => "404",
"page_type" => "error"
),
'view' => '404.php'
);
}