Code Monkey home page Code Monkey logo

ivory's Introduction

logo.png

Powered by Open Swoole: high-performance http micro PHP library

$app = new Ivory\Application();

$app->setHost('0.0.0.0')->setPort(8000);

$app->start();

// Ivory http server listening on http://0.0.0.0:8000

Routing:

$app->get('/', HomeController::class);

Group routes

$app->group('/api', function (Router $router) {
  $router->get('/name', GetController::class);
  $router->post('/name', SaveController::class);
  $router->put('/name', UpdateController::class);
  $router->delete('/name', DeleteController::class);
});

Dynamic routes

$app->get('/api/weather/city/:city', GetWeatherController::class);

Use case driven controllers:

class HomeController {
  public function execute(Request $request): string {
    return 'This is my response';
  }
}

Customize the response

class GetUserController {
  public function execute(Request $request, Response $response): array {
    $response->header("Content-Type", "application/json");

    return [
      'user' => User::first()
    ];
  }
}

Bind services/controllers to the D.I. container:

$app->bind(Service::class, function () {
  return new Service();
});

$app->bind(HomeController::class, function (Container $c) {
  return new HomeController(
    service: $c->get(GenerateNameService::class)
  );
});

Dependencies will be injected to the constructor:

class HomeController {
  public function __construct(protected Service $service) {
    //
  }
}

Global middlewares

// before the request is handled
$app->addPreGlobalMiddleware(CheckIPMiddleware::class);

// after the request is handled
$app->addPostGlobalMiddleware(LogRequestMiddleware::class);

Controller middlewares

// third argument in router definition
$app->get('/name', NameController::class, [NameMiddleware::class]);

ivory's People

Contributors

dannyyassine 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.