danidoble / routing
A easy way to start with symfony-routing
Installs: 59
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:plugin
Requires
- php: ^8.1
- ext-json: *
- illuminate/support: ^v10.0.3
- ramsey/uuid: ^4.7.3
- symfony/routing: ^v6.2.5
Requires (Dev)
- spatie/ignition: ^1.4.3
- vlucas/phpdotenv: ^v5.5.0
Suggests
- spatie/ignition: Render errors in a beautilful page
- symfony/error-handler: Render errors in page
README
A simple way to stat with symfony/routing
PHP 8.1 is required for version ^v1.0
composer require danidoble/routing
Debug::enable();
This is only for debug mode, change it for a custom page,
check symfony/error-handler
Make a file named .env
with
APP_URL="https://localhost/"
APP_DEBUG=true
APP_VIEW_DIR="views"
change localhost for your url site
use Symfony\Component\ErrorHandler\Debug; use Danidoble\Routing\Route; use Danidoble\Routing\Testing; include __DIR__ . "/vendor/autoload.php"; // This is only for debug mode, change it for a custom page, check [symfony/error-handler](https://github.com/symfony/error-handler) Debug::enable(); $base_path = __DIR__ . DIRECTORY_SEPARATOR; // base path of your project $base_view_path = env('APP_VIEW_DIR','views'); // route of your directory of views \Danidoble\Routing\View::$BASE_VIEW_PATH = $base_path . env('APP_VIEW_DIR'); // example: /home/your/project/views $dotenv = Dotenv\Dotenv::createImmutable(__DIR__"); $dotenv->load();
Instantiate Route of \Danidoble\Routing\Route;
$routes = new Route(); //add routes $routes->add('/', [Testing::class, 'index'])->setMethods(['GET','POST']); //only get and post allowed $routes->add('/help', [Testing::class, 'index'])->setMethods('GET'); //only get allowed $routes->add('/danidoble', [Testing::class, 'index', 'a']); // all methods allowed // dispatch $routes->dispatch();
Creating route example
$path = "/lorem";//route slug $controller = \Danidoble\Routing\Testing::class; // class/controller $method = "index"; // method to execute $route_name = "example_route"; // name of route $routes->add($path,[$controller,$method,$route_name]);
In your controller
class Testing extends \Danidoble\Routing\Controller { public function index() { //dump($this->getParent()); //dump($this->param('demo')); dump($this->getParams()); } }
If you want to overwrite __construct()
be sure to apply some code like below
public function __construct(RequestContext $_context, RouteCollection $_routes, UrlMatcher $_matcher, Route $_parent) { parent::__construct($_context, $_routes, $_matcher, $_parent); // ... your code here }