Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Aug 7, 2023
1 parent d98b563 commit d5c2893
Showing 1 changed file with 33 additions and 53 deletions.
86 changes: 33 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,53 @@ composer require scrawler/router

## ✨ Setup

Note 4.x release changes the way router handles request and response, if you still wanna continue using old way with symfony components goto [3.x branch](https://github.com/scrawler-labs/router/tree/3.x)

```php
<?php

use Scrawler\Router\RouteCollection;
use Scrawler\Router\Router;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;


$dir = /path/to/your/controllers;
$namespace = Namespace\of\your\controllers;
$dir = '/path/to/your/controllers';
$namespace = 'Namespace\of\your\controllers';

$collection = new RouteCollection($dir,$namespace)
$router = new Router();
// Register your directory for automatic routing
$router->register($dir,$namespace);

/**
* As of v3.2.0 you can now enblae route caching and optionally pass your own PSR 16 implementation
* $collection = new RouteCollection($dir,$namespace,true);
* OR with your own cache adapter
* $cache = new Psr\SimpleCache\CacheInterface();
* $collection = new RouteCollection($dir,$namespace,true,$cache);
* you can now also enblae route caching by passing your own PSR 16 implementation
* $cache = new Psr\SimpleCache\CacheInterface();
* $router->enableCache($cache);
**/

$router = new Router($collection);
//Optional you can now pass your own Request object to Router for Router to work on
//$router = new Router($collection,Request $request);
// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
$uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);

//Dispatch route and get back the response
$response = $router->dispatch();
// Or Alternatively you can pass header as optional argument
//$response = $router->dispatch(['content-type' => 'application/json']);


//Do anything with your Response object here
//Probably middleware can hook in here
[$status,$handler,$args,$debug] = $router->dispatch($httpMethod,$uri);
switch ($status){
case \Scrawler\Router\Router::NOT_FOUND:
//handle 404 error
// $debug contains extra debug info useful to check failure in automatic routing
break;
case \Scrawler\Router\Router::METHOD_NOT_ALLOWED:
//handle 405 method not allowed
break;
case \Scrawler\Router\Router::FOUND:
//call the handler
$response = call_user_func($handler,...$args);
// Send Response
//echo $response
}

//send response
$response->send();
```

Done now whatever request occurs it will be automatically routed . You don't have define a single route
Expand Down Expand Up @@ -228,35 +237,6 @@ class Hello
```
<br>

## 🔄 Redirection
If you want to redirect a request you can return a Symphony redirect response
```php
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;

// Inside hello.php
class Hello
{

// set flash messages

public function getAbc()
{
// redirect to external urls
return new RedirectResponse('http://example.com/');

// Or alternatively you can set your arguments in flashbagk and redirect to internal URL
// Note you may use any flash manager to achieve this but as HttpFoundation is already a dependecy of Router here is a exmple with Symfony Session
$session = new Session();
$session->start();
$session->getFlashBag()->add('notice', 'Profile updated');
return new RedirectResponse('http://mydomain.com/profile');

}
}
```
Infact from Scrawler Router 3.1.0 you can directly return object of [\Symfony\Component\HttpFoundation\Response](https://symfony.com/doc/current/components/http_foundation.html#response) from your controller
<br><br>

## 👏 Supporters
If you have reached here consider giving a star to help this project ❤️
Expand Down

0 comments on commit d5c2893

Please sign in to comment.