PHP code example of jijihohococo / ichi-route

1. Go to this page and download the library: Download jijihohococo/ichi-route library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jijihohococo / ichi-route example snippets



composer 

#!/usr/bin/env php


te\Command\RouteCommand;


$routeCommand=new RouteCommand;
$routeCommand->run(__DIR__,$argv);



php ichi make:controller ItemController



$routeCommand = new RouteCommand;
$routeCommand->setPath('new_app/Controllers');
$routeCommand->run(__DIR__,$argv);


use JiJiHoHoCoCo\IchiRoute\Router\Route;

$route=new Route;
$route->get('items',function(){
	echo "show items";
});


$route->get('items','App\Controllers\ItemController@show');



$route->setBaseControllerPath('App\Controllers');



$route->run();



'items/' (GET METHOD)



$route->patch('items','App\Controllers\ItemController@show');



$route->put('items','App\Controllers\ItemController@show');



$route->delete('items','App\Controllers\ItemController@show');



$route->get('items/show/{id}','App\Controllers\ItemController@show');


namespace App\Controllers;

class ItemController{

	// 'items/show/{id}' //
	public function show($id){
		echo $id;
	}
}



$route->get('items/show/{id}',function($id){
	echo $id;
})



'items/show/1' (GET METHOD)
'items/show/2' (GET METHOD)



$route->resource('items','App\Controllers\ItemController');


namespace App\Controllers;

class ItemController{

	// GET METHOD //
	// 'items' //
	public function index(){

	}

	// GET METHOD //
	// 'items/create' //
	public function create(){

	}

	// POST METHOD //
	// 'items/create' //
	public function save(){

	}

	// GET METHOD //
	// 'items/{id}/edit'
	public function edit($id){

	}

	// PUT METHOD //
	// 'items/{id}/edit' //
	public function update($id){

	}

	// DELETE METHOD //
	// 'items/{id}/destroy' //
	public function destroy($id){

	}


}


php ichi make:controller ItemController --resource


	
	'items/' (GET METHOD) // Go to to get items' list
	'items/create' (GET METHOD) // Go to create item
	'items/create' (POST METHOD) // Create items
	'items/1/edit' (GET METHOD) // Go to update item
	'items/1/edit' (PUT METHOD) // Update item
	'items/1/destroy' (DELETE METHOD) // Delete item



php ichi make:controller ItemController --api-resource


	
	'items/' (GET METHOD) // Go to to get items' list
	'items/create' (POST METHOD) // Create items
	'items/1/edit' (GET METHOD) // Go to update item
	'items/1/edit' (PUT METHOD) // Update item
	'items/1/destroy' (DELETE METHOD) // Delete item


$route->group(['url_group'=>'admin'],function(){
	
	$this->get('items','App\Controllers\ItemController@getItems');
	$this->get('brands','App\Controllers\BrandController@getBrands');
});


'admin/items' (GET METHOD)
'admin/brands' (GET METHOD)


use JiJiHoHoCoCo\IchiRoute\Router\Route;

$route = new Route;
$route->setDefaultDomain('your_main_domain.com');



$route->domain('your_subdomain.com',function(){
	$this->get('items','Subdomain/ItemController@get');
});



$route->domain('{subdomain}.com',function(){
	$this->get('items','Subdomain/ItemController@get');
});


namespace App\Controllers\Subdomain;

class ItemController{

	public function get($subdomain){

	}
}


$route->domain('{subdomain}.com',function(){
	$this->get('items',function($subdomain){

	});
})


$route->domain('{subdomain}.{person}.com',function(){
	$this->get('items','Subdomain/ItemController@get');
});


namespace App\Controllers\Subdomain;

class ItemController{

	public function get($subdomain,$person){

	}
}


$route->domain('{subdomain}.{person}.com',function(){
	$this->get('items',function($subdomain,$person){

	});
});



$route->domain('{subdomain}.{person}.com',function(){
	$this->get('items/{id}','Subdomain/ItemController@get');
});


namespace App\Controllers\Subdomain;

class ItemController{

	public function get($subdomain,$person,$id){

	}
}

$route->domain('{subdomain}.{person}.com',function(){
	$this->get('items/{id}',function($subdomain,$person,$id){

	});
});


namespace App\Middlewares;

use JiJiHoHoCoCo\IchiRoute\Middleware\MainMiddleware;

class TestMiddleware extends MainMiddleware{

	public function handle(){
		$subdomainParameters=$this->getDomainParameters();
	}
}

namespace App\Controllers;

use App\Repositories\ItemRepositoryInteface;
class ItemController{

	public $item;
	public function __construct(ItemRepositoryInteface $item){
		$this->item=$item;
	}
}

namespace App\Repositories;

use App\Repositories\{ItemRepositoryInteface,BrandRepositoryInterface};

class ItemRepository implements ItemRepositoryInteface{

	public $brand;

	public function __construct(BrandRepositoryInterface $brand){
		$this->brand=$brand;
	}
}


$route->get('order','App\Controllers\OrderController@order',[
'App\Middlewares\OrderMiddleware'
]);



namespace App\Middlewares;

use JiJiHoHoCoCo\IchiRoute\Middleware\MainMiddleware;

class OrderMiddleware extends MainMiddleware{

	public function handle(){
		//--check your business logic--//
		return $this->next();
	}
}


php ichi make:middleware OrderMiddleware



$routeCommand = new RouteCommand;
$routeCommand->setResourcePath('new_app/Middlewares');
$routeCommand->run(__DIR__,$argv);



$route->get('order','App\Controllers\OrderController@order',[
	'App\Middlewares\LoginMiddleware',
	'App\Middlewares\OrderMiddleware'
]);



$route->get('items/{id}','App\Controllers\ItemController@getItems',[
	'App\Middlewares\CheckItemMiddleware:id'
]);


namespace App\Middlewares;

use JiJiHoHoCoCo\IchiRoute\Middleware\MainMiddleware;

class CheckItemMiddleware extends MainMiddleware{

	public function handle($id){
		//--check your business logic--//
		return $this->next();
	}

}


$route->get('items/{id}/{stock_id}',
	'App\Controllers\ItemController@getItems',[
	'App\Middlewares\CheckItemMiddleware:id,stock_id'
]);


namespace App\Middlewares;

use JiJiHoHoCoCo\IchiRoute\Middleware\MainMiddleware;

class CheckItemMiddleware extends MainMiddleware{

	public function handle($id,$stock){
		//--check your business logic--//
		return $this->next();
	}

}


$route->group(['url_group' => 'admin' , 
	'middleware' => ['App\Middlewares\CheckAdminMiddleware']
 ],function(){
 	$this->resource('items','App\Controllers\ItemController');
 });



$route->group(['middleare' => ['App\Middlewares\CheckUserMiddleware'] ],function(){
	$this->get('order','App\Controllers\OrderController@order');
});



$route->setBaseMiddlewarePath('App\Middlewares');



$route->defaultMiddlewares([
'App\Middlewares\CheckUserMiddleware'
]);



$route->defaultMiddlewares([
'CheckUserMiddleware'
]);



use JiJiHoHoCoCo\IchiRoute\Router\Route;

generateCSRFToken();

$route = new Route;
$route->post('items','App\ItemController@create',[
'JiJiHoHoCoCo\IchiRoute\Middleware\CSRFMiddleware'
]);


use JiJiHoHoCoCo\IchiRoute\Router\Route;

generateCSRFToken();

$route = new Route;
$route->group(['middleare' => 
	['JiJiHoHoCoCo\IchiRoute\Middleware\CSRFMiddleware'] ],function(){
	$this->post('items','App\ItemController@create');
});



$route->get('items_api','App\Controllers\ItemController@getItems',[
	'JiJiHoHoCoCo\IchiRoute\Middleware\APIMiddleware'
]);



$route->get('items_api','App\Controllers\ItemController@getItems',[
	'JiJiHoHoCoCo\IchiRoute\Middleware\CORSMiddleware'
]);


 
use JiJiHoHoCoCo\IchiRoute\Setting\CORS;

CORS::setAvialableSites(['http://domain-one.com','http://domain-two.com']);
// To set Access Control Allow Origins (default is '*')

CORS::setAvailableSitesRegex(['/w3schools/']);
// To set Access Control Allow Origins according to regex

CORS::setAvialableMethods(['GET','POST']);
// To set Access Control Allow Origin Methods (default is '*')

CORS::setAvailableHeaders(['X-Custom-Header','Upgrade-Insecure-Requests']);
// To set Access Control Allow Origin Headers (default is '*')

CORS::setToAllowCredential();
// To set Access Control Allow Credentials to TRUE. (default is 'false')

CORS::setMaxAge(3600);
// To set Access Control Max Age (default is 0)


$route->setPDO($pdoObject,10000);



$route->setPDO($pdoObject);



$route->setRedis($redisObject,10000);



$route->setRedis($redisObject);



$route->setMemcached($memcachedObject,10000);



$route->setMemcached($memcachedObject);



use JiJiHoHoCoCo\IchiRoute\UI\ErrorPage;

echo ErrorPage::show();
exit();


use JiJiHoHoCoCo\IchiRoute\UI\ErrorPage;

echo ErrorPage::show('403 - Unauthorized Request',403);
exit();


use JiJiHoHoCoCo\IchiRoute\UI\ErrorPage;

ErrorPage::setErrorPage('<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>404</h1>
<p>404 URL</p>

</body>
</html>');


use JiJiHoHoCoCo\IchiRoute\UI\ErrorPage;

$errorPage = function($message,$code){
		return <<<HTML
			<html>
			<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
			<title>$message</title>
			<body style="background-color: white;">
			<div style="position: fixed;top:50%;left:50%;transform: translate(-50%, -50%);color:black;font-family: 'Nunito', sans-serif;">
			<p>$message</p>
			</div>
			</body>
			</html>
			HTML;
	};

ErrorPage::setErrorPage($errorPage);
html

<form action=" echo route('items'); 
html

<form action=" echo route('items'); 
html

<form action=" echo route('items'); 
html

<a href=" echo route('items'); 
html

<a href=" echo route('items/show/1'); 
html

<a href=" echo getSubdomainRoute('your_subdomain.com','items'); 
html

<form action=" echo route('items');