Giter Club home page Giter Club logo

aven's Introduction

logo

LICENSE PHP 7 version coverage downloads build

๐Ÿšœ Robust PHP router ๐Ÿšœ

๐Ÿ”ฅ Introduction :

Aven (ayven) is a robust and flexible PHP router For PHP7 and newer versions.

๐Ÿ“Œ Requirements :

  • PHP 7.2 or newer versions
  • PHPUnit >= 8 (for testing purpose)

๐Ÿš€ Installation & Use :

    composer require lotfio/aven

โœ๏ธ Usage :

1- Quick use :

 <?php
    require_once 'vendor/autoload.php';

    // here we are using $_SERVER['REQUEST_URI']
    // you can use $_GET['uri']
    $router = new Aven\Router($_SERVER['REQUEST_URI']);

    $router->get('/', function(){  // with a callback
        return "welcome from aven";
    });

    $router->get('/', "UsersController@method"); // controller method
    $router->get('/', "UsersController::method"); // controller static method


    $router->init(); // initialize router

2- Available routes :

  • GET, POST, ANY, PUT, DELETE, HEAD
 <?php

    $router->get('/',  function(){ return " this is get method"; });
    $router->post('/', function(){ return " this is post method"; });
    $router->any('/',  function(){ return " this is any method allows all"; });

    $router->put('/',    function(){ return " this is put method. you should send $_POST['_method'] = 'put'"; });
    $router->delete('/', function(){ return " this is delete method. you should send $_POST['_method'] = 'delete'"; });
    $router->head('/',   function(){ return " this is head method. you should send $_POST['_method'] = 'head'"; });

3- named routes :

 <?php
    $router->get('/',  function(){ return "this is get named route (default)";})->name('default');

4- redirects :

  • $router->redirect(string $routeName, array $params = [], int $httpcode = 301)
 <?php
    // route 1
    $router->get('/',  function() use($router){  // accessing this route will redirect you to route2 means /hola

        $router->redirect('route2'); // if parametrised route you can pass array of parameters

    })->name('default');

    // route 2
    $router->get('/hola',  function(){ return " welcome to hola from default route";})->name('route2');

5- route parameters :

  • you can use both parenthesis or curly braces for parameters
  • predefind parameters:
    • :int, :integer, :num, :numeric, :number = \d+
    • :str = \w+
    • :alpha = [A-z]+
 <?php

    $router->get('/test/(:int)',  function(){}); // evaluates to /test/\d+
    $router->get('/test/(:str)',  function(){}); // evaluates to /test/\w+

    // optional parameters (if optional parameter uri should end with /)
    $router->get('/test/(:id*)',  function(){}); // optional id /test/ or /test/1
    $router->get('/test/(:id?)',  function(){}); // zero or one id /test/ or /test/0-9

6- custom route parameters :

  • ->regex(array $params)
 <?php
    // override predefined param
    $router->get('/test/(:str)',  function(){})->regex(array(":str"=> '[my-reg-ex]'));

    // custom param
    $router->get('/test/(:hola)',  function(){})->regex(array(":hola"=> '[my-reg-ex]'));

7- route groups :

  • $router->group(string $uri,callable $callback, ?string $groupName)
  • you can have as many nested groups as you want
 <?php

   $router->group('/mygroup', function($router){  // groups adds prefixes to routes

        $router->get('/test',  function(){ return "from /mygroup/test" }); // evaluates to /mygroup/test

   });

    // multiple groups
    $router->group('/group1', function($router){

        $router->group('/group2', function($router){

            $router->get('/test',  function(){ return "from /group1/group2/test" }); // evaluates to /group1/group2/test
        });
   });

8- route namespaces :

  • $router->namespace(string $uri,callable $callback)
  • you can have as many nested namespaces as you want
 <?php

   $router->namespace('My\\Namespace', function($router){  // you can also use dots (My.Namespace) instead of \\
        $router->get('/test',  "TestController@test"}); // evaluates to My\\Namespace\\TestController@test
   });

    // multiple groups
    $router->namespace('ns1', function($router){

        $router->namespace('ns2', function($router){

            $router->get('/test',  "TestController@test"); // evaluates to ns1\\ns2\\TestController@test
        });
   });

9- additionl routes :

  • $router->form(string $uri, $callback|$class, ?array $override, ?string $routeName)
 <?php
    // form route with callback
    $router->form('/login', function(){  }); // works both with GET and POST

    // form route with class
    $router->form('/login', Login::class); // by default class should have showForm & submitForm

    // override default form methods
    $router->form('/login', Login::class, ['get','post']);

    // named form method
    $router->form('/login', Login::class, ['get','post'], 'login.form');

10- additionl routes :

  • $router->crud(string $uri, string $class, ?array $only, ?string $routeName)
 <?php
    // crud route
    // this needs a User class with 4 methods create,read,update,delete
    // create => POST user/create
    // read   => GET with optional pareter user/read/
    // update => PUT with optional pareter user/update/
    // delete => DELETE with optional pareter user/delete/
    $router->crud('/user', User::class);

    // disable some methods
    $router->crud('/user', User::class, ['c']); // only create
    $router->crud('/user', User::class, ['create']); // only create
    $router->crud('/user', User::class, ['c', 'u']); //  create & update
    $router->crud('/user', User::class, ['create', 'update']); //  create & update

    // named crud
    $router->crud('/user', User::class, NULL, 'myCrud'); //  name can be used for redirections

๐Ÿ’ป Contributing

  • Thank you for considering to contribute to Package. All the contribution guidelines are mentioned here.

๐Ÿ“ƒ ChangeLog

๐Ÿบ Support the development

  • Share Package and lets get more stars and more contributors.
  • If this project helped you reduce time to develop, you can give me a cup of coffee :) : Paypal. ๐Ÿ’–

๐Ÿ“‹ License

  • Package is an open-source software licensed under the MIT license.

aven's People

Contributors

lotfio avatar peter279k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

aven's Issues

autoload problem

PHP Warning: require(../autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\silver\aven\aven on line 10

Warning: require(../autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\silver\aven\aven on line 10
PHP Fatal error: require(): Failed opening required '../autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\silver\aven\aven on line 10

Fatal error: require(): Failed opening required '../autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\silver\aven\aven on line 10

Collection routing missing

We need to add Route::crud() or Route::collection() that contain reserved methods:
Create, Update, Store, index, get, all, delete inside the controller

Look up for those methods or give error if something is missing by options to set via route ->allow(['change',...]);

Route::crud('/news', 'NewsController')->grant('change');
or
Route::crud('/news', 'NewsController')->grant(['change', 'middlewear:index']);

Allow /Grantmethod execute command to something pass

  • We can same time make another helper chain method deny

Suggestion for fast memory usage

Change methods from private to const
WHY: This solution works little bit faster and then u need to use \in_array(self::methods) for search the GET POST PUT ... etc...

private $methods = [
        'GET',
        'POST',
        'PUT',
        'PATCH',
        'DELETE',
        'COPY',
        'HEAD',
        'OPTIONS',
        'LINK',
        'UNLINK',
        'PURGE',
        'LOCK',
        'UNLOCK',
        'PROPFIND',
        'ANY',
    ];

to

const METHODS = [
        'GET',
        'POST',
        'PUT',
        'PATCH',
        'DELETE',
        'COPY',
        'HEAD',
        'OPTIONS',
        'LINK',
        'UNLINK',
        'PURGE',
        'LOCK',
        'UNLOCK',
        'PROPFIND',
        'ANY',
    ];

\in_array(self::METHODS)

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.