Using the database to manage blog content is okay but makes writing difficult at times. Let’s use markdown to create a blog on yii framework 1.
Setting Up Composer Autoload
I’m assuming composer is already setup as per yii’s installation. You need to get the PHPMarkdown package with:
composer require michelf/php-markdown
The to make sure the classes from composer are autoloaded, add the following to yii’s index.php
entry point.
// Add composer packages
$loader = require(__DIR__ . '/../vendor/autoload.php');
Yii::$classMap = $loader->getClassMap();
Now creating the simple blog post.
Creating a simple blog post with PHP Markdown in Yii Framework 1
So we will need to read the contents of a markdown file sitting on the server, preferably in a non public location like <root>/protected/data/blog/my-blog.md
.
So let us add that file:
## Yay
This is my first blog post
Now in the action of the BlogController
that you will need to create, we need to read the file contents and then convert that to html with PHPMarkdown and then send that html to the view file.
$markdown=file_get_contents(dirname(__FILE__) . "/../data/blog/my-blog.md",'r');
$html = Markdown::defaultTransform($markdown);
$this->render('index', array('article' => $html));
The final step is showing the htmlified contents of the blog post in the view file index.php
: