kiwi
kiwi is a minimalistic and lightweight blog framework for php., (*1)
Documentation
Kiwi can be installed via Composer:, (*2)
composer create-project jakobjohansson/kiwi
Configuration
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., (*3)
After this is done, you should head to the /migrate route to migrate the default SQL tables., (*4)
Routes
Custom routes can be set in the app/routes.php
file, pointing a route towards a controller and a target method:, (*5)
<?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');
Controllers
All controllers reside in the app/Controllers
directory. An example controller is included with the following methods:, (*6)
<?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!, (*7)
Middleware
You can apply custom middleware by creating a middleware()
method in your controller. It will run on every request directed towards the controller., (*8)
Views
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., (*9)
Templating
Kiwi supports templating similar to that of Laravel Blade:, (*10)
<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, (*11)
Validation
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:, (*12)
<?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
- email
- url
- digits
- alpha, (*13)
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!, (*14)
License
Kiwi is MIT licensed, meaning you are free modify and do as you wish with the code., (*15)