Skip to content

Model-View-Controller web server. Simple yet powerful, built in PHP 🗺️

License

Notifications You must be signed in to change notification settings

YobertyAlej/genesis

Repository files navigation

Genesis MVC 🗺️

License: MIT GitHub code size in bytes GitHub issues

Model-View-Controller web server. Simple yet powerful, built in PHP

Installation

  • Clone (or fork) this repo to your terminal
git clone git@github.com:<your-username>/genesis.git
  • Rename (or copy) the config.example.ini file as config.ini
cd genesis
mv config.example.ini config.ini
  • Add your local configuration

  • Run Composer (global composer)

composer start

Usage

Adding Routes

Create a route to point out what action you want to perform to the web server

Open the file src/Http/Routes.php

We are returning an array of routes assigned either to a Closure function or to a named method in a Controller

Closure

/**
 * src/Http/Routes.php
 * Route defined as an array
 */
    [
        'method' => 'get',
        'name' => 'index',
        'path' => '/',
        'handler' => function () {
          /**
           * This code will be executed when receiving
           * a 'get' http request pointing to the
           * url '/' get
           */
          echo "Hello World";
        }
    ]

This will be the output

Response::view

You can call the Response::view method to return a view file from the src/Views folder named as a the only parameter in the method

/**
 * src/Http/Routes.php
 * Route calling a handler returning a view
 */
    return [
      [
        'method' => 'get',
        'name' => 'index',
        'path' => '/',
        'handler' => function () {
          /**
            * The view method in \App\Http\Response, takes a view name
            * as required argument and search for that file
            * in the \src\Views folder, and any optional
            * parameter can be passed by in the
            * second argument array
            */

            \App\Http\Response::view('hello-world', ['name' => 'Jhon Doe']);
        }
      ]
    ];

The file src/Views/hello-world.php will be returned to the screen and the name variable will be injected to the view

Named Controller

You can also define a Controller to handle all the logic for an entry point, say '/shops'.

Create a file at the Controllers folder (default is src/Http/Controllers) and define a static method, which name will be requested in the Routes.php file in the next format {ControllerName}@{Controller Method}, e.g.:

/**
 * src/Http/Routes.php
 */

$controllerNamespace = 'App\Http\Controllers\\';

    return [
      [
        'method' => 'get',
        'name' => 'controllerIndex',
        'path' => '/controllerIndex',
        'handler' => $controllerNamespace.'ShopController@products'
      ]
    ];
/**
 * src/Http/Controllers/ShopController.php
 */

<?php

namespace App\Http\Controllers;

use App\Http\Response;
use App\Acme;

class ShopController
{
    public static function products()
    {
      /**
       * Basic controller method implementation
       *
       * Returns a content
       */

      $products = Acme::getProducts();
      return Response::view('shops/products', ["products" => $products]);
    }

}

This way you will be encapsulating the business logic in that controllers file, and you can call your Models from here to feed the views with information

License

MIT

About

Model-View-Controller web server. Simple yet powerful, built in PHP 🗺️

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published