PHP code example of zanko-khaledi / php-simple-router
1. Go to this page and download the library: Download zanko-khaledi/php-simple-router library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zanko-khaledi / php-simple-router example snippets
use ZankoKhaledi\PhpSimpleRouter\Request;
use ZankoKhaledi\PhpSimpleRouter\Router;
,function (Request $request){
echo "foo route";
});
Router::executeRoutes();
use App\Controllers\FooController;
use ZankoKhaledi\PhpSimpleRouter\Router;
oo',[FooController::class,'store']);
Router::executeRoutes();
use ZankoKhaledi\PhpSimpleRouter\Request;
use ZankoKhaledi\PhpSimpleRouter\Router;
Router::get('/foo/{file}',function (Request $request){
echo $request->params()->file;
})->where('/foo\/[a-z]+/');
Router::executeRoutes();
use ZankoKhaledi\PhpSimpleRouter\RouterCollection;
;
// Modules/Product/routes/product.php
use ZankoKhaledi\PhpSimpleRouter\Router;
use ZankoKhaledi\PhpSimpleRouter\Request;
Router::get('/products',function (Request $request){
// your code
});
use ZankoKhaledi\PhpSimpleRouter\Interfaces\IMiddleware;
use ZankoKhaledi\PhpSimpleRouter\Interfaces\IRequest;
class AuthMiddleware implements IMiddleware
{
public function handle(IRequest $request,Callable $next)
{
if(!isset($_SESSION['admin']) && $_SESSION['admin'] !== 'zanko'){
header("Location:/");
exit();
}
$next($request);
}
}
use App\Middlewares\AuthMiddleware;
use ZankoKhaledi\PhpSimpleRouter\Router;
use ZankoKhaledi\PhpSimpleRouter\Request;
outes();
use ZankoKhaledi\PhpSimpleRouter\Request;
use ZankoKhaledi\PhpSimpleRouter\Router;
use ZankoKhaledi\PhpSimpleRouter\Interfaces\IRoute;
});
Router::executeRoutes();
use ZankoKhaledi\PhpSimpleRouter\Request;
use ZankoKhaledi\PhpSimpleRouter\Router;
use App\Middelwares\AuthMiddleware;
use App\Controllers\FooController;
$request->params()->id;
});
Router::post('/foo',[FooController::class,'store']);
});
Router::executeRoutes();
use ZankoKhaledi\PhpSimpleRouter\Request;
use ZankoKhaledi\PhpSimpleRouter\Router;
use App\Middelwares\AuthMiddleware;
(){
// code
});
Router::executeRoutes();
use ZankoKhaledi\PhpSimpleRouter\Request;
use ZankoKhaledi\PhpSimpleRouter\Router;
use ZankoKhaledi\PhpSimpleRouter\RouterCollection;
use App\Middelwares\AuthMiddleware;
=> [AuthMiddleware::class]]
,function (){
// code
});
Router::executeRoutes();
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use ZankoKhaledi\PhpSimpleRouter\Router;
class Test extends TestCase
{
protected string $baseUri = 'http://localhost:8000';
public function test_get_route()
{
$request = Router::assertRequest($this->baseUri);
$response = $request->assertGet('/foo',[]);
$this->assertEquals(200,$response->status());
$this->assertJson($response->json());
$this->assertSame(json_encode([
'name' => 'Zanko'
]),$response->body());
}
public function test_foo_post_route()
{
$request = Router::assertRequest($this->baseUri);
$response = $request->assertPost('/foo',[
'form_params' => [
'name' => 'Teddy'
]
]);
$this->assertEquals(201,$response->status());
$this->assertJson($response->json());
$this->assertSame(json_encode([
'name' => 'Foo'
]),$response->json());
}
}
use ZankoKhaledi\PhpSimpleRouter\Router;
$baseUri = 'http://localhost:8000';
$request = Router::assertRequest($baseUri) // config base uri for sending requests to server
$request->assertGet($route,[]); // route like /foo
$request->assertPost($route,[]);
$request->assertPut($route,[]);
$request->assertPatch($route,[]);
$request->assertDelete($route,[]);
bash
$ composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.