<?php

use think\facade\Route;
use think\Request;

Route::rule('article/query_article', "article/query", "GET|POST");
Route::get('article/:id', "article/detail");
Route::get('articles/:category_id', "article/index");
Route::get('page/:category_id', "page/index");

// 验证码
Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
// 单应用模式下 多模块间兼容 模块/控制器/操作的访问模式 等同于 模块.控制器/操作
Route::group(function (Request $request) {
    // 控制器必需采用大驼峰,方法必需采用小驼峰
    $baseUrl = $request->baseUrl();
    $baseUrl = explode('.htm', $baseUrl)[0];
    $urlArr  = explode('/', ltrim($baseUrl, '/'));
    if (!strpos($baseUrl, '.')) {
        $length  = count($urlArr);
        $dirList = [];
        if ($length >= 4) {
            $module     = $urlArr[0];
            $dirList    = array_slice($urlArr, 1, $length - 3);
            $controller = $urlArr[$length - 2] ?? 'index';
            $action     = $urlArr[$length - 1] ?? 'index';
        } else {
            $module     = '';
            $controller = $urlArr[0] ?? 'index';
            $action     = $urlArr[1] ?? 'index';

            if (in_array($urlArr[0], ['api', 'manager'])) {
                $module     = $urlArr[0];
                $controller = $urlArr[1] ?? 'index';
                $action     = $urlArr[2] ?? 'index';
            }
        }

        $dir = '';
        if (!empty($dirList)) {
            foreach ($dirList as $d) {
                $dir .= toCamelString($d, true).'.';
            }
        }

        // 模块(支持多级目录).控制器/操作  如:/api/aa-bb-cc/dd-ee-ff/gg-hh-ii/controller/action
        //  将访问 api.aaBbCc.ddEeFf.ggHhIi.controller/action
        $moduleSeparate = !empty($module) ? '.' : '';
        $route          = $module.$moduleSeparate.$dir.toCamelString($controller).'/'.toCamelString($action, true);

        Route::rule($baseUrl, $route);
    }
});