kiwi is a minimalistic and lightweight blog framework for php.
Kiwi can be installed via Composer:
composer create-project jakobjohansson/kiwi
Included in the package is a .env.example
file, which should be renamed to .env
and omitted from version control. It is a simple environment file containing fields for database settings, user settings and more general application settings. Kiwi will not run without this file.
After this is done, you should head to the /migrate route to migrate the default SQL tables.
Custom routes can be set in the app/routes.php
file, pointing a route towards a controller and a target method:
<?php
/*
* Routes file.
* You can create your own custom routes in here, at the end of this file.
*/
$router->get('', 'PageController/index');
$router->get('post/{id}', 'PageController/show');
All controllers reside in the app/Controllers
directory. An example controller is included with the following methods:
<?php
/**
* Render the main page.
*
* @return void
*/
public function index()
{
View::render('index', ['posts' => Post::all()]);
}
/**
* Render a specific post page.
*
* @param Post $post
*
* @return void
*/
public function show(Post $post)
{
if (!$post) {
throw new HttpException("That post doesn't exist.");
}
View::render('post', ['post' => $post]);
}
Notice the type hinted Post parameter in the show method. It will be automatically injected when you provide a wildcard in your route!
You can apply custom middleware by creating a middleware()
method in your controller. It will run on every request directed towards the controller.
As seen in the example above, views can be requested from a controller method by stating View::render($viewpath, $arrayOfData)
. The view path is relative to the app/Views
folder, with a suffixed .view.php
added at the end, meaning you can simply enter the file name.
Kiwi supports templating similar to that of Laravel Blade:
<div class="content">
@foreach ($posts as $post)
<h3 class="subtitle is-4">
<a href="/post/{{$post->id}}">
{{$post->title}}
</a>
</h3>
<div class="content">
{{nl2br($post->body)}}
</div>
<footer>
<small>Written {{$post->created_at}}.</small>
</footer>
@endforeach
</div>
At the time of writing, the following directives are supported:
- if / elseif / else statements
- echo expressions
- foreach loops
- includes
Validation is rather simple with kiwi. Simply access a form field using the Input
class, which takes the field name as the first parameter and an array of rules as the second parameter:
<?php
$post->title = Input::field(
'title',
[
'required' => 'The title is required.',
'min:3' => 'The title needs to be atleast 3 characters long.',
]
);
$post->save();
The supported rules can be found in the Rule
class. At the time of writing, the following rules are supported:
- min
- max
- required
- url
- digits
- alpha
If validation fails, kiwi will redirect back with access to an $errors
variable, containing all the data you need to display an informative error message!
Kiwi is MIT licensed, meaning you are free modify and do as you wish with the code.