Code Monkey home page Code Monkey logo

mini-framework's Introduction

MiniFramework

An exercise on how to make an MVC framework from (not really) scratch.

Environment

The framework uses the package vlucas/phpdotenv for environment secret's management.

Environment are stored in the .env file. Use the ENV[$key]super global or the env($key, $default = null) global function to access the individual environment values.

Container

A simple IoC container.

Objects can be bound to the container:

Application::bind(
    $object, 
    $name // opcional
);

For complex objects a Closure can be used:

Application::bind(function(){
    return (new Object())
        ->doSomethingElse();
});

Objects can be resolved:

Or using the helper:

Application::resolve($name);

Here's the list of objects bound by the Application:

  • env
  • config
  • template
  • router

The can be resolved using the keyword:

Application::resolve('config');

Routes

For routes the package bramus/router is used.

Add routes in app/routes.php calling the verb method, with the path as a first parameter and a closure as a second parameter:

use Core\Application;

$router = Application::resolve('router');

$router->get('/', function() { 
    // your code here
});

Methods available:

  • get
  • post
  • put
  • patch
  • delete

Views

Views use the package thephpleague/plates.

You can add views inside the resources\Views folder.

To render the view the view helper can be used. The first parameter must be the name of view's file (without the .php) and the second parameter is an array with available arguments that will become available inside the view:

use Core\Application;

$router = Application::resolve('router');

$router->get('/', function() { 
    
    view('home', [
        'foo' => 'var',
    ]);

});

Config

Configuration values can be stored in the App/config.php file. Environment values can be used inside for composing values:

return [
    'title' => env('TITLE', 'MiniFramework 2'),
];

To access the config the helper can be used.

$this->get('/', function (){
    
    $title = config('title');

});

Todo

  • Middleware
  • Models
  • Request
  • Singletons for the container
  • 404 view

mini-framework's People

Contributors

juaniquillo avatar

Watchers

 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.