Giter VIP home page Giter VIP logo

moxrouter's Introduction

MoxRouter

A super simple and fast PHP router

Installation

It's recommended that you use Composer to install MoxRouter.

$ composer require moxxie/moxrouter "^0.2.0"

Usage

Getting started

Create an index.php file with the following contents:

<?php
require 'vendor/autoload.php';
 
$router = new Moxxie\MoxRouter();
 
$router->get('/{message}', function($message){
  echo "Hello " . $message . "!";
});
 
$router->run();

You can test this using the built-in server that comes with PHP:

$ php -S localhost:8000

http://localhost:8000/world will now display "Hello, world!".

Routing your requests

MoxRouter supports GET, POST, PUT, PATCH, and DELETE HTTP requests

// Will only match GET HTTP requests
$router->get('/product/{id}', function($id){
  // Return product with id = $id
});
 
// Will only match POST HTTP requests
$router->post('/product', function(){
  // Create new a product
});
 
// Will only match PUT HTTP requests
$router->put('/product/{id}', function($id){
  // Update product with id = $id
});
 
// Will only match PATCH HTTP requests
$router->patch('/product/{id}', function($id){
  // Apply changes made to product with id = $id
});
 
// Will only match DELETE HTTP requests
$router->delete('/product/{id}', function($id){
  // Delete product with id = $id
});
 

Service Container

<?php
$router = new Moxxie\MoxRouter();
  
// Create an empty container
$container = [];
 
// Add a service to the container
$container['service'] = function(){
  return new Service();
};

$router->get('/', function(){
  // Use the new Service
  $service = $this->service();
});

// Run the router with the container
$router->run($container);

Hooks

Before route

$router->before(function(){
  // This code will be executed before a route has been executed
});

After route

$router->after(function(){
  // This code will be executed after a route has been executed
});

Not found(404)

You can override the default 404 handler

$router->notFound(function(){
  // This code will be executed when a route is not found
});

Rewrite all requests to MoxRouter

Apache .htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

Nginx

try_files $uri /index.php;

License

(MIT License)

Copyright (c) 2017 Moxxie - https://github.com/Moxxie/MoxRouter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

moxrouter's People

Contributors

moxxie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

moxrouter's Issues

Add support for query parameters

Hello,

routes that include query parameters, i.e. a question mark, do not seem to work.

$router->get('/some/page?foo={bar}', function($bar){
    // Some instructions...
});

When I try to access http://example.com/some/page?foo=bar, it goes to the notFound route. But if I replace the ?foo= by a /, then the route is executed.

Would it be possible to add support for query parameters? Thank you!

Main route not working since release 0.2.4

Hello,

I've been using your router for a few months now, it works great! Problem is, when I update it to version 0.2.5, my main route is ignored, and the notFound route is used instead when I try to access the index page. When I revert back to version 0.2.3, the main route is not ignored.

To be clear, this is ignored in versions 0.2.4 and 0.2.5 :

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

And this route is loaded instead:

$router->notFound(function(){});

When I try to access https://example.com/.

Nothing else changes in my code. Tested on PHP 7.2.4 and 7.3.8.

Thank you for your help.

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.