Download the PHP package pedroquezado/router without Composer
On this page you can find all versions of the php package pedroquezado/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pedroquezado/router
More information about pedroquezado/router
Files in pedroquezado/router
Package router
Short Description Um sistema de roteamento flexível e poderoso para PHP.
License MIT
Homepage http://www.upinside.com.br
Informations about the package router
Router - PedroQuezado
About PedroQuezado
PedroQuezado/Router is a PHP router component designed for routing requests in your application or API. It provides a simple and intuitive way to define routes and handle HTTP verbs (GET, POST, PUT, PATCH, DELETE) using the MVC pattern. The router works independently, ensuring isolation and seamless integration into your projects.
Installation
You can install the PedroQuezado Router via Composer. Run the following command:
Router Class
The Router class is the main entry point for routing requests. It provides methods to define routes, handle different HTTP verbs, and run the router to match and execute the appropriate route handlers.
Usage
To use the Router class in your application, follow these steps:
- Create an index.php file in your project's root directory.
- Use Composer's autoloader to load the required files.
- Instantiate the Router class and define your routes.
- Dispatch the routes.
Here's an example of how to use the Router class:
In the example above, we instantiate the Router class, define different routes using the get(), post(), put(), and delete() methods, and finally call the run() method to dispatch the routes.
Methods
get($path, $callback)
Defines a route for HTTP GET requests.
- $path: The path of the route.
-
$callback: The callback function or class method to execute when the route matches.
post($path, $callback)
Defines a route for HTTP POST requests.
- $path: The path of the route.
- $callback: The callback function or class method to execute when the route matches.
put($path, $callback)
Defines a route for HTTP PUT requests.
- $path: The path of the route.
- $callback: The callback function or class method to execute when the route matches.
patch($path, $callback)
Defines a route for HTTP PATCH requests.
- $path: The path of the route.
- $callback: The callback function or class method to execute when the route matches.
delete($path, $callback)
Defines a route for HTTP DELETE requests.
- $path: The path of the route.
- $callback: The callback function or class method to execute when the route matches.
Note: The $path parameter can include placeholders enclosed in curly braces {} to capture dynamic segments of the URL. These placeholders can be accessed in the callback function or class method as parameters.
Constructor
The Router class constructor accepts the base URL as its parameter. This base URL is used for generating the correct route URLs in your application. Here's an example of creating a new Router instance:
Defining Routes
Here's an example of defining a GET route that maps to a closure function:
Class method callback:
In the class method callback format, the class and method names are separated by the double-colon (::) syntax.
Namespaces
The namespace() method allows you to define a namespace for the class or classes that will be referenced in the callbacks. This is useful for organizing classes into different directories or namespaces. Here's an example of how it can be used:
In this example, the UserController and ProductController classes are located in the App\Controllers namespace.
Middleware
middleware($callback): Define um middleware para ser executado antes de uma rota.
Group
group($options, $callback): Define um grupo de rotas para compartilhar configurações comuns.
Dispatching Routes
Once you have defined your routes, you can call the run() method to dispatch the routes and handle incoming requests.
The run() method will match the current request URL and HTTP method to the defined routes and execute the corresponding callback function or class method.
Contributing
Contributions are welcome! If you would like to contribute to the PedroQuezado Router, feel free to open an issue or submit a pull request. We appreciate your feedback and contributions to make this project even better.
License This project is licensed under the MIT License.