Giter Club home page Giter Club logo

php-routingsystem's Introduction

PHP-RoutingSystem | PHP Framework for MVC pattern implementation

Index

Installation

PHP-RoutingSystem requires PHP 5.3+ and its only dependencies (at this moment) are defined in the composer.json file. You just need to define the public folder as the root of your site and thats all you need to start working with this framework... well and run:

composer install

To download all the dependencies.

Configuration

Configuration file

PHP-RoutingSystem uses a mechanisms named Drivers (located in core/drivers) to add functionalities to your app. So the configuration must be managed by the Driver named Config.php. Te configuration driver will load your configuration from "config.ini", file that must be placed in the root of the project, and basically must contain:

[defaults]
controller = ""
[db]
host = ""
user = ""
password = ""
name = ""

If you want to place more configurations you have to define them inside your own section, and they can be accessed:

Drivers\Config::get()->var("section.var", "default");

Friendly URLs

Apache

#.htaccess
<IfModule mod_rewrite.c>
	Options -MultiViews
	Options +FollowSymLinks
	RewriteEngine On
</IfModule>

<IfModule mod_rewrite.c>
	ReWriteBase /
	RewriteRule ^static - [L]
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>

Nginx

Pending...

URL Mapping

In PHP-RoutingSystem every url will be mapped to a controller following the next patterns:

Common controllers

API controllers

The API controllers follow a RESTFUL pattern, so an API controller will have method that represents a HTTP verb directly. The verbs and methods are:

  • GET => show
  • POST => create
  • PUT => update
  • DELETE => delete

The way that an url is mapped for API controllers is made using the word "api" on the url. i.e.

Controllers

Common

All the controllers must be located in app/controllers folder, following the next convention:

If you want to create a new controller for your home page you have to create a new file named:

Home.php

And fill it with:

<?php

namespace Controllers;

use Core;
use Models;

class Home extends Core\Controller {

    public function index() {
        echo "Home page!"
    }

}

If you have not defined the "Home" controller as your default controller you can access to it in this way:

http://youredomain.com/home

This url will automatically launch the index method in the controller, and you will see the content that you print inside of it.

API

The API controllers must be located on the api folder inside the app/controllers folder. And the structure for a new API controller is:

<?php

namespace Controllers\API;

use Core;
use Models;

class Example extends Core\APIController {
    public function show() {
        
    }
    public function create() {
        
    }
    public function create() {
        
    }
    public function delete() {
        
    }
}

Note: It is not neccessary to define all methods, only those that are going to be used.

Views

###Templates

PHP-RoutingSystem uses Twig as its template engine, but it's already integrated inside the framework's flow and file structure.

To make use of this mechanism it is neccessary to create the corresponding template files for each view inside the app/templates folder. i.e.

If you want to create a home template you may create a folder named "home" inside templates and then a file named "index.html", and then use:

\View::renderHTML("home/index", array("var1" => $var1));

So the framework will load the correspondig file and pass the array of vars according the Twig documentation.

View classes

The framework allow you to create custom view classes wich may be useful to present data in very specific ways. All the view classes must be placed inside app/views folder and every view is linked to a controller. If you create a view class for your Home controller it'll look like:

<?php

namespace Views;

class Home {

    public function homePage($name) {
        echo "This is the home page!";
    }

}

Then, you can call this class from your controller in any method with:

$mView = $this->getView();

And access the required method from the view with:

$mView->homePage();

Models

All the models must be placed in app/models and work with php-activerecord library.

An example of this classes could be the model for "users" table (following the php-activerecord conventions) in your database. And it would be:

<?php

namespace Models;

use ActiveRecord;

class User extends ActiveRecord\Model {

}

Donate

paypal

php-routingsystem's People

Contributors

svazqz avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.