Download the PHP package jijihohococo/ichi-route without Composer
On this page you can find all versions of the php package jijihohococo/ichi-route. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jijihohococo/ichi-route
More information about jijihohococo/ichi-route
Files in jijihohococo/ichi-route
Package ichi-route
Short Description Router Library For PHP Projects
License MIT
Informations about the package ichi-route
ICHI PHP ROUTER
ICHI PHP ROUTER is aimed to be the fast PHP Router with ease to use and protecting the security vulnerabilities
License
This package is Open Source According to MIT license
Table Of Content
- ICHI PHP ROUTER
- License
- Table Of Content
- Installation
- Setting
- Apache
- Nginx
- Creating Controller
- Single Route
- PATCH Method
- PUT Method
- DELETE Method
- Using Routes
- Route Parameter
- Resource Route
- Prefix Route
- Subdomain Route
- Single Subdomain
- Subdomain Parameter
- Dependency Injection
- Middleware
- CSRF Token Authentication
- API Request Authentication
- CORS
- Caching Route
- Caching with Database
- Caching with Redis
- Caching with Memcached
- Error Page Customization
Installation
Setting
You must create following htaccess file before setting route according to your server.
Apache
Create .htaccess file in your based folder
Nginx
Create .htacess file in your based folder
Creating Controller
You must create the controller for your routes.
Firstly you need to created the file named "ichi" under your project folder and use the below code in this file
And then you can create the controller in your commandline
The default file folder is "app/Controllers". So after making command, the controller you created will be in the this default file folder. If you want to change the default folder path, you can change it in your "ichi" file.
Single Route
You can add routes with "get","post","put","delete" and "head" functions.
Each function represents the each route method
You can add route with closure function or the function of controller class.
With closure function
With Controller class
You must autoload your the controller folder before using the route function in your composer.json
If you have the same controller path for all controllers, you can set base controller path for all routes before adding routes.
To run all of your routes, you must use "run()" function.
You must use "run()" function after declaring all routes for your system
After running route function, the routes (URL) are able to run
PATCH Method
To make PATCH method , you need request and add 'PATCH' values in this. And the action must 'POST'.
In your route
For the form data
PUT Method
To make PUT method , you need request and add 'PUT' values in this. And the action must 'POST'.
In your route
For the form data
DELETE Method
To make DELETE method , you need request and add 'DELETE' values in this. And the action must 'POST'.
In your route
For the form data
Using Routes
Calling routes in frontend
Route Parameter
In many cases, you have a time to make route parameters
In your controller class
You can also do with closure function
After running route function, the routes (URL) are able to run
Calling routes in frontend
Resource Route
You can CRUD routes with one route method
In your controller class
You can also create the resource controller in terminal after making according to the insturctions from Creating Controller
After running route function, the routes (URL) are able to run
You can also create the api resource controller in terminal after making according to the insturctions from Creating Controller
After running route function, the routes (URL) are able to run
Prefix Route
You can use prefix route to make groups
So the below url are able to use
You can add the routes in the group closure function.
Don't include '/' in declaring "url_group"
Subdomain Route
You must set your main domain name before declaring routes if you want to use subdomains.
Single Subdomain
You can set your subdomain routes with "domain()" function
You can use all route functions within "domain()" function
You can't use domain function within group function
If you want to show your subdomain route, use getSubdomainRoute function. First parameter is domain name and second parameter is declared route under this subdomain.
Subdomain Parameter
If you want to use parameter in your subdomain
Parameters are embrace with "{" and "}" as route parameters
You must pass subdomain parameters into the closure function or class's function
Class's function
Closure function
If you want to use multiple subdomain parameters, use '.' between parameters and string
Class's function
Closure function
If you want to use route parameters in subdomain parameters, You must pass subdomain parameters and also route parameters in your class's function or closure function
Class's function
Closure function
If you want to get subdomain parameters in your middleware which are under that subdomain routes
In your middleware class
You can't pass subdomain parameters into your middleware like you can do with your route parameters
Dependency Injection
You can make dependency injection with controller class.
You must have interface and class according to the below format
Your interface and class must be autoloaded
Interface | Class |
---|---|
ItemInterface | Item |
ItemRepositoryInterface | Item Repository |
The functions of class that dependency injected will automatically run
In your controller class
You can also add another dependency injection in your repositories.
Middleware
You can add middlewares in single route like below
You must delcare middleware class
Your middleware classes must be autoloaded in composer as we mentioned before
You must extend JiJiHoHoCoCo\IchiRoute\Middleware\MainMiddleware and add "handle()" function. In your "handle" function, you must always return "next()" function. You can check your business transactions in this "handle" function.
You can create the middleware via terminal after creating "ichi" file as we mentioned in Creating Controller
The default path for observer is "app/Middlewares". You can also change this in "ichi" file.
You can add multiple middleware classes
Those middlewares will be loaded sequently because of using "next()" function in each "handle()" function.
You can add parameters in middleware with your route parameters
In your middleware class
You can add multiple parameters in middleware with your route parameters
In your middleware class
You can add middlewares in prefix routes like the way you do in single routes and parameter routes
You can add only middleware in prefix routes.
If you have the same middleware path for all middlewares, you can set base middleware path for all routes before adding routes.
There are some middlewares that already written in this library. For those middlewares you must declare their middleware path completely.
If you have the middlewares that you want to check for all routes
If you don't delcare base middleware path
If you declare base middleware path
CSRF Token Authentication
You can protect create and update route with CSRF Token Authentication
You must generate CSRF Token before declaring the routes
You can add JiJiHoHoCoCo\IchiRoute\Middleware\CSRFMiddleware in your prefix routes too.
In your frontend php file
API Request Authentication
You can add middleware to accept only API request for your route
To pass JiJiHoHoCoCo\IchiRoute\Middleware\APIMiddleware, you must add "application/json" value in your "Content-Type" header when you request that api route
CORS
You can make CORS to get your data from another domains
You can make options for your CORS with JiJiHoHoCoCo\IchiRoute\Setting\CORS
Caching Route
You can cache parameter routes with
- Database
- Redis
- Memcached
You must do following instructions before adding routes
Caching with Database
You must run this SQL code to create "ichi_routes" table in your database
And add your pdo object with expired time in seconds
You can also make without expired time
Caching with Redis
Add your redis object with expired time in seconds
You can also make without expired time
Caching with Memcached
Add your memcached object with expired time in seconds
You can also make without expired time
Error Page Customization
You can call the error page (whatever it is default or customized)
Default eror message is "404 - URL is not found". Default HTTP response code is 404.
You can customize error message and HTTP response code
If you don't like the default error page from this library,you can show the own error page
If you want to show more specific error according to your HTTP response code, you must create a function
In your customized function, you must have "$message" and "$code" parameters