Download the PHP package devamirul/p-router without Composer

On this page you can find all versions of the php package devamirul/p-router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package p-router

p-router

A simple, lightweight, and powerful PHP Router with rich features like Middleware and Controllers. Heavily inspired by the way Laravel handles routing.

Features:

Table of Contents:

Installation:

Installation is possible using Composer.

Add the following script to composer.json file:

Run the following command:

Create app folder.

Start PHP server.

Examples:

Basic route:

or

Dynamic Route:

Middleware:

You can do method chaining if you want.

Directories:

app/config: This folder contains system config files. Make your changes only in the config file.

app/Middlewares: Create your custom middlewares in this folder.

app/Controllers: Create your custom controllers in this folder.

Routes:

Available Router Methods:

The router allows you to register routes that respond to any HTTP verb:

Basic Routes:

Routes accept a URI and a closure or a array, providing a very simple and expressive method of defining routes and behavior without complicated routing configuration files.

  1. First create index.php file.
  2. Define Application root path.
  3. Require vendor autoload file.
  4. Create router singleton instance.
  5. Define routes.
  6. Run the application via the run() method.

Or create a separate route.php file and include that file in the index.php file.

First create route.php or name the file according to your choice:

Include route.php in the index.php file:

Let's discuss the second parameter. The second parameter accepts a closure or an array of key value pairs. The 'key' of the array will be a class and the value will be a method of the class, the method will be invoked by the class.

Use return instead of echo.

Named Routes:

Named routes allow the convenient generation of URLs or redirects for specific routes. You may specify a name for a route by chaining the name method onto the route definition:

Route names should always be unique.

Generating URLs To Named Routes

Once you assign a name to a given route, you can redirect via toRoute():

If the named route defines parameters, you may pass the parameters as the second argument to the toRoute function. The given parameters will automatically be inserted into the generated URL in their correct positions:

If you pass additional parameters in the array, those key / value pairs will automatically be added to the generated URL's query string:

*If you use an asterisk () in the route, you cannot call it via toRoute().**

Route Parameters:

Required Parameters:

Sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user's ID from the URL. You can get it through $request->getParam() method.

You may define as many route parameters as required by your route:

Route parameters will always start with Colon ':' and should contain alphanumeric characters.

Parameters:

Automatically get 'Request' instances in your route callback or controller.

In callback:

In controller:

Optional Parameters:

Occasionally you may need to specify a route parameter that may not always be present in the URI. You may do so by placing a question sign ? mark after the parameter:

It is important to note that optional parameter are always placed at the end of the URLs.

Regular Expression Constraints:

You can restrict the format of your route parameter by using the where method on a route instance. The where() method takes a regular expression as parameter which determines how the parameter should be delimited. The "where()" method will accept the serialized parameters of the router's dynamic parameters:

Regular expression constraints for optional parameter:

Regular expression constraints for multiple optional parameters:

match and any method

Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the match method. Or, you may even register a route that responds to all HTTP verbs using the any method:

Route wildcard:

You can use dynamic routes using asterisks:

In the example above, you can dynamically use any path after admin/. The asterisk is used as a wildcard and matches any combination of characters.

Middlewares:

app/Middlewares: Middleware provides a convenient mechanism for inspecting and filtering HTTP requests entering your application.

The predefined middleware files are:- AuthMiddleware.php CsrfMiddleware.php

By default you will get a request instance in the handle method.

Make middleware:

To create a new middleware, use the composer middleware command:

The command line interface will ask you for a middleware name, you enter a name. It will automatically add "Middleware" to the name you provided. For example, you want to create a middleware named "example". Then your middleware class will be ExampleMiddleware.php

For example, This framework includes a middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware will redirect the user to your application's login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.

Add middleware

After creating the middleware add it to the middleware array in the 'app/config/middleware.php' file. Add your own middleware to this list and assign it an alias of your choice:

If you would like to assign middleware to specific routes, you may invoke the middleware method when defining the route. Once the middleware alias is defined, you use the alias when assigning middleware to routes:

You can assign multiple middleware at once if you want:

Set default middlewares:

If you want to set some middleware to Https verbs by default, you can do that very easily, The defined middleware will run when that https method request is handled:

Open app/config/middleware.php

CSRF Protection:

Anytime you define a "POST", "PUT", "PATCH", or "DELETE" HTML form in your application, you should include a hidden CSRF _token field in the form so that the CSRF protection middleware can validate the request, Otherwise, the request will be rejected. For convenience, you may use the setCsrf() function to generate the hidden token input field:

Controllers:

app/Controllers: Controllers respond to user actions (submitting forms, show users, view data, and any action etc.). Controllers are classes that extend the BaseController class.

By default you will get request instance in each method.

Make controller

To create a new controller, use the composer controller command:

The command line interface will ask you for a controller name, you enter a name. It will automatically add "Controller" to the name you provided. For example you want to create a controller named "example". Then your controller class will be ExampleController.php

Request:

Framework's Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input that were submitted with the request.

Accessing The Request:

You can get request instance through the request helper function:

Also you will get methods.

isGet() isPost() isPut() isPatch() isDelete()

Handle Html View Content File:

You can easily view html content from a controller or callback function.

Simply `require' the file you want to view:

In Content file:

You can easily view the data in the content file:

In Content file:

Handle Form:

Method Field:

Since HTML forms can't make PUT, PATCH, or DELETE requests, you will need to add a hidden _method field to spoof these HTTP verbs. The setMethod() Blade directive can create this field for you:

Helpers:

Table of Contents

General Helpers:

Get config data:

View the data in details then exit the code:

View the data in details:

Form Helpers:

Set new CSRF value:

Example:

Check CSRF is valid or not, return bool:

Set form method, like put/patch/delete:

Example:

Request Helpers:

Get request instance

Example:

Response Helpers:

Redirect link:

Finds route by route name and redirect this route:


All versions of p-router with dependencies

PHP Build Version
Package Version
Requires php Version >=8.00
ext-json Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package devamirul/p-router contains the following files

Loading the files please wait ....