1. Go to this page and download the library: Download juneszh/alight 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/ */
return [
'route' => [
//Import on any request
'*' => 'config/route/web.php',
//Import when requesting admin.yourdomain.com
'admin' => 'config/route/admin.php',
//Import multiple files when requesting api.yourdomain.com
'api' => ['config/route/api.php', 'config/route/api_mobile.php'],
]
];
Alight\Route::get($pattern, $handler);
// Example
Alight\Route::get('/', 'Controller::index');
Alight\Route::get('/', ['Controller', 'index']);
// Or try this to easy trigger hints from IDE
Alight\Route::get('/', [Controller::class, 'index']);
// With default args
Alight\Route::get('post/list[/{page}]', [Controller::class, 'list'], ['page' => 1]);
// Common HTTP request methods
Alight\Route::options('/', 'handler');
Alight\Route::head('/', 'handler');
Alight\Route::post('/', 'handler');
Alight\Route::delete('/', 'handler');
Alight\Route::put('/', 'handler');
Alight\Route::patch('/', 'handler');
// Map for Custom methods
Alight\Route::map(['GET', 'POST'], 'test', 'handler');
// Any for all common methods
Alight\Route::any('test', 'handler');
// Matches /user/42, but not /user/xyz
Alight\Route::get('user/{id:\d+}', 'handler');
// Matches /user/foobar, but not /user/foo/bar
Alight\Route::get('user/{name}', 'handler');
// Matches /user/foo/bar as well, using wildcards
Alight\Route::get('user/{name:.+}', 'handler');
// The /{name} suffix is optional
Alight\Route::get('user[/{name}]', 'handler');
// Root wildcards for single page app
Alight\Route::get('/{path:.*}', 'handler');
// For example log every hit request
Alight\Route::beforeHandler([svc\Request::class, 'log']);
Alight\Route::get('test', 'handler');
Alight\Route::post('test', 'handler');
// Effective in the current route file
Alight\Route::disableCache();
// Cache one day
Alight\Route::get('about/us', 'handler')->cache(86400);
// Or force disable cache
Alight\Route::put('user/info', 'handler')->cache(0);
// Define a global authorization verification handler
Alight\Route::authHandler([\svc\Auth::class, 'verify']);
// Enable verification in routes
Alight\Route::get('user/info', 'handler')->auth();
Alight\Route::get('user/password', 'handler')->auth();
// No verification by default
Alight\Route::get('about/us', 'handler');
// In general, routing with authorization will not use browser cache
// So auth() has built-in cache(0) to force disable cache
// Please add cache(n) after auth() to override the configuration if you need
Alight\Route::get('user/rank/list', 'handler')->auth()->cache(3600);
namespace svc;
class Auth
{
public static function verify()
{
// Some codes about get user session from cookie or anywhere
// Returns the user id if authorization is valid
// Otherwise returns 0 or something else for failure
// Then use Router::getAuthId() in the route handler to get this id again
return $userId;
}
}
// Cooldown only takes effect when authorized
Alight\Route::put('user/info', 'handler')->auth()->cd(2);
Alight\Route::post('user/status', 'handler')->auth()->cd(2);
// Domains in config will receive the common cors header
Alight\Route::put('share/config', 'handler')->cors();
// The specified domain will receive the common cors header
Alight\Route::put('share/specified', 'handler')->cors('abc.com');
// The specified domain will receive the specified cors header
Alight\Route::put('share/specified2', 'handler')->cors('abc.com', 'Authorization', ['GET', 'POST']);
// All domains will receive a 'Access-Control-Allow-Origin: *' header
Alight\Route::put('share/all/http', 'handler')->cors('*');
// All domains will receive a 'Access-Control-Allow-Origin: [From Origin]' header
Alight\Route::put('share/all/https', 'handler')->cors('origin');
// Initializes the default cache
$cache = \Alight\Cache::init();
// Initializes others cache with key
$cache2 = \Alight\Cache::init('redis');
// Use SimpleCache(PSR-16) interface
if (!$cache->has('test')){
$cache->set('test', 'hello world!', 3600);
}
$cacheData = $cache->get('test');
$cache->delete('test');
$cache6 = \Alight\Cache::psr6('memcached');
$cacheItem = $cache6->getItem('test');
if (!$cacheItem->isHit()){
$cacheItem->expiresAfter(3600);
$cacheItem->set('hello world!');
// Bind to a tag
$cacheItem->tag('alight');
}
$cacheData = $cacheItem->get();
$cache6->deleteItem('test');
// Delete all cached items in the same tag
$cache6->invalidateTags('alight')
// Or symfony/cache adapter style
$cacheData = $cache6->get('test', function ($item){
$item->expiresAfter(3600);
return 'hello world!';
});
$cache6->delete('test');
namespace svc;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
class Cache
{
public static function adapter(array $config)
{
switch ($config['type']) {
case 'apcu':
return new ApcuAdapter();
break;
case 'array':
return new ArrayAdapter($config['defaultLifetime']);
default:
return new NullAdapter();
break;
}
}
}
namespace svc;
class Error
{
public static function catch(Throwable $exception)
{
// Some code like sending an email or using Sentry or something
}
public static function page(int $status)
{
switch ($status) {
case 400:
// Page code...
break;
case 401:
// Page code...
break;
case 403:
// Page code...
break;
case 404:
// Page code...
break;
case 500:
// Page code...
break;
default:
// Page code...
break;
}
}
}
// Suppose the absolute path of the project is /var/www/my_project/
\Alight\App::root('public/favicon.ico'); // /var/www/my_project/public/favicon.ico
// Of course, you can also use absolute path files with the first character '/'
\Alight\App::root('/var/data/config/web.php');