Category: Yii Framework

Adding Composer Autoload to Yii Framework 1 and using PHPMarkdown

Using the database to manage blog content sucks, 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:

 yii-markdown-blog-post

Source: Adding composer to yii stackoverflow

Setting up Yii Environment on your Server

How to Setup Yii on your Environment Server

1. Make Sure your vhost is setup correctly, with debian/ubuntu that is /etc/apache2/sites-available/sitename



Options FollowSymLinks -Indexes Includes
AllowOverride All

2. Set your include path in php.ini that is etc/php/apache2/php.ini

; UNIX: "/path1:/path2"
include_path = ".:/usr/share/library"

3. Download, get url from Yii Download Page

wget https://github.com/yiisoft/yii/releases/download/1.1.14/yii-1.1.14.f0fee9.tar.gz

4. Unzip

tar -xzvf file.tar.gz

5. Change name, Set Permissions, Restart Apache:


mv yii-1.14e2938u123 yii
chown www-data:www-data yii/
chmod -R 755 yii/
service apache2 restart

6. Finally you still need to give the exact directory in index.php of the yii.php:


$yii=dirname(__FILE__).'/../../../usr/share/library/yii/framework/yii.php';