测试
This commit is contained in:
80
config/helpers.php
Normal file
80
config/helpers.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 路由注册封装
|
||||
* 使用 cc_route_register 函数,路由名需要用连词符 "-" 来表示,并且控制器要使用小驼峰的命名规范
|
||||
* @param $router
|
||||
* @param $class
|
||||
* @return void
|
||||
*/
|
||||
|
||||
if ( !function_exists('cc_route_register') ) {
|
||||
function cc_route_register ($router,$class): void
|
||||
{
|
||||
foreach ( $router as [ $method, $router_name ] ) {
|
||||
// ds(Illuminate\Support\Facades\Route::$method( $router_name, [$class, conjunction_symbol_processing($router_name)] ));
|
||||
Illuminate\Support\Facades\Route::$method( $router_name, [$class, conjunction_symbol_processing($router_name)] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动路由注册封装
|
||||
* 使用 cc_auto_route_register 函数,路由名需要用连词符 "-" 来表示,并且控制器要使用小驼峰的命名规范
|
||||
* @param $class
|
||||
* @return void
|
||||
*/
|
||||
if ( !function_exists('cc_auto_route_register') ) {
|
||||
function cc_auto_route_register ($class): void
|
||||
{
|
||||
foreach ($class as $key => $value) {
|
||||
|
||||
// 获取控制器$value的所有方法
|
||||
$methods = (new ReflectionClass($value))->getMethods();
|
||||
|
||||
// 注册路由
|
||||
foreach ($methods as $method) {
|
||||
// 获取方法注释 @Method
|
||||
$docComment = $method->getDocComment();
|
||||
if ($method->name === '__construct' || preg_match('/@Method\s+(NO)\b/', $docComment, $matches)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$httpMethod = 'any';
|
||||
// 查询 @Method GET 或者 @Method POST
|
||||
if (preg_match('/@Method\s+(GET|POST|PUT|DELETE|PATCH|OPTIONS|HEAD|ANY)\b/', $docComment, $matches)) {
|
||||
$httpMethod = $matches[1];
|
||||
}
|
||||
Illuminate\Support\Facades\Route::$httpMethod( $key. '/'. cc_camel_case_to_dash($method->getName()), [$value, conjunction_symbol_processing($method->name)] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 连赐福转换小驼峰
|
||||
* @param $string
|
||||
* @return string
|
||||
*/
|
||||
if(!function_exists('conjunction_symbol_processing')) {
|
||||
function conjunction_symbol_processing($string): string
|
||||
{
|
||||
$string = str_replace('-', ' ', $string); // 将连字符替换为空格
|
||||
$string = ucwords($string); // 将每个单词的首字母大写
|
||||
$string = str_replace(' ', '', $string); // 空格删除
|
||||
$string[0] = lcfirst($string)[0]; // 首字母小写
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 小驼峰转换-
|
||||
if ( !function_exists('cc_camel_case_to_dash') ) {
|
||||
function cc_camel_case_to_dash($str): string
|
||||
{
|
||||
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1-$2', $str));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user