-
-
Notifications
You must be signed in to change notification settings - Fork 46
2. Getting Started
İzni Burak Demirtaş edited this page Jan 23, 2022
·
7 revisions
composer.json file:
{
"require": {
"izniburak/router": "^2.0"
}
}
after run the install command.
$ composer install
OR run the following command directly.
$ composer require izniburak/router
require 'vendor/autoload.php';
use Buki\Router\Router;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$router = new Router;
// For basic GET URI
$router->get('/', function(Request $request, Response $response) {
$response->setContent('Hello World');
return $response;
# OR
# return 'Hello World!';
});
// For basic GET URI by using a Controller class.
$router->get('/test', 'TestController@main');
// For auto discovering all methods and URIs
$router->controller('/users', 'UserController');
$router->run();
And add this code to the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Congratulations! Now, you can use PHP-Router.
Also, you can check Symfony HTTP Foundation to get more information about Request and Response instance.
If you have a problem, you can contact me or open new issue.
Default configurations of Buki\Router class for namespaces and paths are as follows;
[
'paths' => [
'controllers' => 'Controllers',
'middlewares' => 'Middlewares',
],
'namespaces' => [
'controllers' => '',
'middlewares' => '',
]
]
So,
- Controllers path: Controllers
- Middlewares path: Middlewares
- Controllers namespaces: none
- Middlewares namespaces: none
If you would like to define paths and namespaces for Controllers and Middlewares, you can do this at below:
require 'vendor/autoload.php';
$router = new \Buki\Router\Router([
'paths' => [
'controllers' => 'app/Controllers',
'middlewares' => 'app/Middlewares',
],
'namespaces' => [
'controllers' => 'App\Controllers',
'middlewares' => 'App\Middlewares',
]
]);
$router->get('/', function() {
return 'Hello World!';
});
$router->run();
[
'base_folder' => '',
'main_method' => 'main',
'paths' => [
'controllers' => 'Controllers',
'middlewares' => 'Middlewares',
],
'namespaces' => [
'controllers' => '',
'middlewares' => '',
],
'debug' => false,
]
All settings that you can configure are as above.
- base_folder => Application base folder. (For example: Directory where index.php file of your app) Default value: getcwd();
- main_method => This option is defining main method that the routes defined by controller() method for main route of the controller.
- paths => Controllers and Middlewares paths of your Application.
- namespaces => Controllers and Middlewares namespaces of your Application.
- debug => Debug Mode. To show errors and exceptions details. (Default=false)