Quite possibly the smallest MVC framework you'll ever use.

Related tags

Frameworks Swiftlet
Overview

Swiftlet Build Status

Swiftlet is quite possibly the smallest MVC framework you'll ever use. And it's swift.

Licensed under the MIT license.

Buzzword compliance

Micro-Framework
Loosely coupled
Unit tested
Namespaced
Pluggable
Composer
PSR-4
PHP7
MVC
OOP

✘ ORM

Installation

  • Clone (or download and extract) Swiftlet into a directory on your PHP supported web server.
  • Having Composer installed, run composer dump-autoload.

Getting started: controllers and views

Let's create a page. Each page consists of a controller and at least one view.

The controller does most of the work; views should be limited to simple presentation logic (loops and switches).

Controller src/HelloWorld/Controllers/Foo.php

<?php
namespace HelloWorld\Controllers;

use \Swiftlet\Abstracts\Controller as ControllerAbstract;

class Foo extends ControllerAbstract
{
	protected $title = 'Foo'; // Optional but usually desired 

	// Default action
	public function index(array $args = [])
	{
		// Pass a variable to the view
		$this->view->helloWorld = 'Hello world!';
	}
}

Important: class names are written in CamelCase and match their filename.

View views/foo.php

<h1><?= $this->pageTitle ?></h1>

<p>
	<?= $this->helloWorld ?>
</p>

The controller can set variables directly on the view. Values are automatically made safe for use in HTML, use $this->get('variable', false) on values that should be treated as code.

You can now view the page by navigating to http://<swiftlet>/foo in your web browser!

If you get a "404 Not Found" you will need to enable rewrites in the web server configuration. Alternatively you can navigate to http://<swiftlet>?q=foo.

Swiftlet can be invoked from the command line (e.g. to run cron jobs). Simply run php public/index.php -q foo.

Routing

Notice how you can access the page at /foo by simply creating a controller named Foo. The application maps URLs to controllers, actions and arguments.

Consider this URL: /foo/bar

In this case foo becomes the name of the controller and view and bar the name of the action. Actions are public methods on the controller class.

You can specify a different view for an action using $this->view->setName(). The view name is a filename relative to the src\<namespace>\views directory, without the .php suffix.

If the controller or action is not specified they default to index (/ will call index() on \HelloWorld\Controller\Index).

Underscores in the controller name are translated to directory separators, so /foo_bar will point to src/HelloWorld/Controllers/Foo/Bar.php.

Dashes in routes are ignored; /foo-bar/baz-qux calls bazqux() on \HelloWorld\Controllers\Foobar.

Custom routes

Automatic routing is convenient but more granular control is often desirable. In these cases custom routes can be defined.

A route maps a URL to an action (method).

URL segments can be replaced with a "wildcard" placeholder (a variable name prefixed with a colon). This value becomes available for use in the controller.

Consider this route: bar/:qux

Navigating to <controller>/bar/something matches this route. The value of $args['qux'] becomes something.

<?php
namespace HelloWorld\Controllers;

use \Swiftlet\Abstracts\Controller as ControllerAbstract;

class Foo extends ControllerAbstract
{
	protected $routes = array(
		'hello/world' => 'index',
		'bar/:qux'    => 'bar'
		);

	public function index(array $args = [])
	{
		// You navigated to foo/hello/world
	}

	public function bar(array $args = [])
	{
		// You navigated to foo/bar/<something>
		// $args['qux'] contains the second URL argument
	}
}

Models

Let's throw a model into the mix and update the controller.

Model src/HelloWorld/Models/Foo.php

<?php
namespace HelloWorld\Models;

use \Swiftlet\Abstracts\Model as ModelAbstract;

class Foo extends ModelAbstract
{
	public function getHelloWorld()
	{
		return 'Hello world!';
	}
}

Controller src/HelloWorld/Controllers/Foo.php

<?php
namespace HelloWorld\Controllers;

use \Swiftlet\Abstracts\Controller as ControllerAbstract;
use \HelloWorld\Models\Example as ExampleModel;

class Foo extends ControllerAbstract;
{
	protected $title = 'Foo';

	public function index()
	{
		// Get an instance of the Example class 
		// See src/HelloWorld/Models/Example.php
		$example = new ExampleModel;

		$this->view->helloWorld = $example->getHelloWorld();
	}
}

A model typically represents data. This can be an entry in a database or an object such as a user.

<?php
use \HelloWorld\Models\User as UserModel;

$user = new UserModel;

$user->setEmail('[email protected]');

$user->save();

Loading and saving data should almost always happen in a model. You can create as many models as you like; they aren't tied to controllers or views.

Events and listeners

Listeners listen for events. When an event is triggered all relevant listeners are called and can be used to extend functionality.

Swiftlet has a few core events and additiontal ones can be triggered pretty much anywhere using $this->app->trigger($event).

Listener src/HelloWorld/Listeners/Foo.php

<?php
namespace HelloWorld\Listeners;

use \Swiftlet\Abstracts\Controller as ControllerAbstract;
use \Swiftlet\Abstracts\Listener as ListenerAbstract;
use \Swiftlet\Abstracts\View as ViewAbstract;

class Foo extends ListernerAbstract
{
	public function actionAfter(ControllerAbstract $controller, ViewAbstract $view)
	{
		// Overwrite our previously set "helloWorld" variable
		$view->helloWorld = 'Hi world!';
	}
}

This listener listens for the core actionAfter event and changes the view variable helloWorld from our previous example to Hi world!.

Listeners don't need to be installed or activated, all files in the src/HelloWorld/Listeners/ directory are automatically included and their classes instantiated. Listeners are called in alphabetical order.

The core events are:

  • actionBefore
    Called before each action

  • actionAfter Called after each action

Libraries

Reusable components such as code to send an email or generate a thumbnail image should go in a separate library class.

<?php
use \HelloWorld\Libraries\Email as EmailLibrary;

$email = new EmailLibrary;

$email->send($to, $subject, $message);

Configuration

No configuration is needed to run Swiftlet. If you're writing a model that does require configuration, e.g. credentials to establish a database connection, you may use the application's setConfig and getConfig methods:

<?php
$this->app->setConfig('variable', 'value');

$value = $this->app->getConfig('variable');

Values can be set in config/main.php or a custom file.

Public methods

Application Swiftlet\App

  • App dispatchController()
    Determine which controller to use and run it

  • App serve()
    Serve the page

  • mixed getConfig(string $variable)
    Get a configuration value

  • App setConfig(string $variable, mixed $value)
    Set a configuration value

  • App registerHook(string $hookName, array $params)
    Register a hook

View Swiftlet\View

  • mixed get(string $variable [, bool $htmlEncode = true ])
    Get a view variable, encoded for safe use in HTML by default

  • View set(string $variable [, mixed $value ])
    Set a view variable

  • mixed get(string $variable [, bool $htmlEncode ])
    Get a view variable, pass false as the second parameter to prevent values from being HTML encoded.

  • string getRootPath()
    Absolute client-side path to the website root

  • mixed htmlEncode(mixed $value)
    Recursively make a value safe for HTML

  • mixed htmlDecode(mixed $value)
    Recursively decode a previously encoded value to be rendered as HTML

  • View render(string $path)
    Render the view

Comments
  • can't login....

    can't login....

    i have installed framework on my system but when i try to login in it username n password (sysPassword) it doesnt login in......plz help me..... im new to this framework want to learn more on this framework....

    opened by vijaykoogu 17
  • Route method

    Route method

    Thank you for this framework, I really like it, and fit my requirements for my new project I have quick question, how to handle different route method such as get, post, etc in controller?

    opened by rayanwv 13
  • Make proper use of namespaces

    Make proper use of namespaces

    Hi, just stumbled upon your framework and am quite interested in using it for some smaller projects, the tiny footprint is very neat especially considering that it still manages to fulfil the basic needs of a framework.

    One thing I don't like about it though is that you're not actually utilising namespaces, the only use you get out of them right now is that you will be able to easily embed your framework with other libraries or even other frameworks, but that's not a very feasible scenario to begin with.

    You're still forcing users to use class names like "IndexController" whilst namespaces would allow you to just name it Index within the namespace Swiftlet\Controllers. Personally I would much favour this approach as I've never been a fan of mixing the namespace inside the class name, sure it works.. and it's easy with legacy IDE's.. but then what's the point of using namespaces to begin with.

    Anyway, would love to hear what your thoughts are on this, looking forward to seeing where this framework might go.

    opened by Naatan 8
  • url parsing in pages.

    url parsing in pages.

    Don't really know if this issue is made by me or by swiftlet, but when i enter a link in page plugin, it returns in html as : localpath/url instead of url. In the database its normal url, ... latest alfa version of swiflet

    • svenn
    opened by ghost 6
  • case sensitive routing

    case sensitive routing

    Just a notice that the app is case-sensitive for routing. Views /test and /Test are not the same. Controllers will be the same but not views so this should be discussed.

    opened by va5ja 5
  • $_rootPath

    $_rootPath

    In the App::run() function the $_rootPath doesn't get set if the FILE an $_SERVER['DOCUMENT_ROOT'] are not the same which can be a case of a virtual directory.

    opened by va5ja 5
  • InnoDB max bytes limitation

    InnoDB max bytes limitation

    I noticed that the 'plugin' field length is 256 as stated here, which raised an error on my local MySQL server (and maybe for others too??). I've fixed this by changing 'plugin' field length to 255.

    Specified key was too long; max key length is 767 bytes
    

    PS : I love Swiftlet's simplicity anyway :)

    opened by iromli 5
  • weird google findings

    weird google findings

    For some reason google seems to get linked to these kind of links; for some reason... any idea http://www.***.be/$1 http://www.***.be/page//aanmeld_hulp?permalink=aanmeld_hulp

    opened by ghost 5
  • getController()

    getController()

    Big changes last days, have to get used to it :) Where should be the lib folder, probably in the root. Anyways, I have one idea. At the moment you can get either from View or Application the name of the view in string. This is useful if you want to know which View you are in for example. But there is no function to get the name of a controller like getName() in Controller or getController() in the Application. Would be great to also get the name of the Controller somehow in string.

    opened by va5ja 4
  • View file structure

    View file structure

    I think it would be also great to sort View file structure to Controller and View. At the moment you get from IndexController and indexAction just index.html.php. It's a bit hard and too much work to separate each View by the action inside a file, would be just better to separate the view into index_action.html.php or something since you can't call two actions at same time anyway.

    opened by va5ja 4
  • database : INT vs TINTYINT

    database : INT vs TINTYINT

    I don't know if this is a real "issue" but I wondered why the field owner in user plugin, is actually a INT(1) while a tintyInt seems more then enough for the bool function it has. Now, I am a really noob at designing database so sorry if I mis a point here.

    opened by ghost 4
Owner
Elbert Alias
Elbert Alias
PHP Kafka client is used in PHP-FPM and Swoole. PHP Kafka client supports 50 APIs, which might be one that supports the most message types ever.

longlang/phpkafka Introduction English | 简体中文 PHP Kafka client is used in PHP-FPM and Swoole. The communication protocol is based on the JSON file in

Swoole Project 235 Dec 31, 2022
a framework for WebDevelop based on the mvc structure. The name of this project for Fun because everyone can use it. Completely simple and powerful structure for all your projects

A_A (-.-) ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ |-| █▄─▄▄─█▄─██─▄█─▄▄▄▄█─▄▄▄▄█▄─█─▄█─▄▄▄─██▀▄─██─▄

MasihGhaznavi 7 Jun 29, 2022
TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs.

TrailLamp Introduction TrailLamp is a lightweight, easy-to-use Php MVC framework that can be used to build web applications and REST APIs. Installatio

Etorojah Okon 14 Jun 10, 2022
a micro mvc framework for php

micro-mvc-php a micro mvc framework for php Config your Web url in .env . lifecycle All request proccess by index.php Autoload files include in bootst

Amiranbari 6 Jul 9, 2022
Symprowire is a PHP MVC Framework based and built on Symfony, using the ProcessWire CMS as DBAL and Service Provider.

Symprowire - PHP MVC Framework for ProcessWire 3.x Symprowire is a PHP MVC Framework based and built on Symfony using ProcessWire 3.x as DBAL and Serv

Luis Mendez 7 Jan 16, 2022
The Hive is a simple php mvc framework

Hive framework The Hive is a simple php mvc framework . Information Features : -MVC design -PDO connection -OOP system -Twig template -Very Fast, simp

Mohammad Maleki 2 Sep 4, 2021
FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Ruby on Rails.

FlyCubePHP FlyCubePHP is an MVC Web Framework developed in PHP and repeating the ideology and principles of building WEB applications, embedded in Rub

Anton 1 Dec 21, 2021
💡 Mudrock is a MVC PHP framework, which was inspired by the Laravel and CodeIgniter frameworks.

?? Mudrock is a MVC PHP framework, which was inspired by the Laravel and CodeIgniter frameworks

null 3 Nov 17, 2021
This repository include my own PHP MVC Framework

PHP OWN MVC FRAMEWORK Kendimi geliştirmek ve modern PHP Framework'lerinin işleyişini kavram amacıyla inşa ettiğim profesyonele yakın PHP MVC Framework

Yılmaz Kadan 9 Nov 24, 2022
Minimal PHP MVC Framework that is eternally broken.

▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄ ██ ▄▄▄ █ ▄▄▀█ ▄▄██▄██ ▄▀██▄██ ▄▄▀█ ▄▄▀ ██ ███ █ ▄▄▀█▄▄▀██ ▄█ █ ██ ▄█ ▀▀ █ ██ ██ ▀▀▀ █▄▄▄▄█▄▄▄█▄▄▄█▄▄██▄▄▄█▄██▄█

Paul (hxii) Glushak 3 Dec 16, 2021
A Slim PHP MVC framework built just for fun!

Aura Framework A Slim PHP MVC framework built just for fun! en: Note: This repository only contains the core code of the Aura framework. If you want t

Murilo Magalhães Barreto 2 Dec 16, 2021
Slim Framework skeleton application with MVC Schema

Slim Framework skeleton application with MVC Schema

JingwenTian 9 Apr 29, 2021
A simple PHP MVC framework without extra files and codes that you don't need

Welcome to (SPM) Simple PHP MVC, just what you need! This is a simple PHP MVC framework without extra files and codes that you don't need.

Van Hudson Galvoso 5 Sep 17, 2022
PHP MVC Framework

You can select version on branch list. cmd> composer install 0.1. Z Framework (V2.0.0) 0.2. Easiest, fastest PHP framework. (Simple) 0.3. Document 1.

Mustafa Ömer ESER 2 Jan 4, 2023
A super fast, customizable and lightweight PHP MVC Starter Framework to extend for your own...

PHPMVC A super fast, customizable and lightweight PHP MVC Starter Framework to extend for your own... How to Start Clone this repo - git clone https:/

Maniruzzaman Akash 9 Dec 11, 2022
A simle MVC framework implimentation using php

Vanilla-framwork A simle MVC framework implimentation using php , no additonal 3rd party are used (Vanilla Php); Email Support Configuration for email

null 3 Aug 15, 2022
Bootcamp project based on PHP-MVC using MySQL database.

Up-Stream This is the implementation of a full website based on PHP MVC. Using MySql database to create a website. And Bootstrap4 for front-end. Start

AmirH.Najafizadeh 4 Jul 31, 2022
Trabajo 06 Laravel y el modelo MVC

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Miguel Angel Sotelo Palacios 1 Nov 15, 2021