因为我们把业务代码单独放在了addons,因此每个应用的每个请求都必须设置路由规则才能正常访问。
应用的路由文件必须命名为route.php,并且放在应用的根目录下,路由规则定义完全遵循thinkphp6的。
例如demo应用的路由文件为demo/route.php,其中路由规则定义如下:
<?php use think\facade\Route; //应用的路由文件需要返回函数,函数内部实现可完全参考tp6的路由定义规则 $addon = get_addon_name(__FILE__, 2); $namespace = "\addons\\$addon"; return function () use ($addon, $namespace){ $admin_namespace = $namespace . "\admin\controller\\"; Route::group("$addon/admin/uploader", function () use ($admin_namespace) { $controller = $admin_namespace . "Uploader::"; Route::rule('editorpost', $controller . "editorpost"); Route::rule('filepost', $controller . "filepost"); Route::rule('picturepost', $controller . "picturepost"); }); Route::group("$addon/admin/index", function () use ($admin_namespace) { $controller = $admin_namespace . "Index::"; Route::rule('welcome', $controller . "welcome"); Route::rule('index', $controller . "index"); }); Route::group("$addon/admin/article", function () use ($admin_namespace) { $controller = $admin_namespace . "Article::"; Route::rule('index', $controller . "index"); Route::get('add', $controller . "add"); Route::get('edit', $controller . "edit"); Route::post('setstatus', $controller . "setstatus"); Route::post('savepost', $controller . "savepost"); }); $home_namespace = $namespace . "\home\controller\\"; Route::group("$addon/home/index", function () use ($home_namespace) { $controller = $home_namespace . "Index::"; Route::rule('index', $controller . "index"); }); };