Slim Framework - 将代码拆分为 index.php 以外的多个文件
Slim Framework - splitting code into multiple files other than index.php
在 Slim Framework 的 documentation 上,它说
In this example application, all the routes are in index.php but in
practice this can make for a rather long and unwieldy file! It’s fine
to refactor your application to put routes into a different file or
files, or just register a set of routes with callbacks that are
actually declared elsewhere.
虽然它没有说明如何实际执行此操作。我唯一的想法是,您可以将代码拆分为多个 PHP 文件,然后在 index.php
中使用 include
或 require
来引用这些文件。
我也不确定 "register a set of routes with callbacks that are actually declared elsewhere"
是什么意思
有没有人对此有任何想法,因为我想要构建的应用程序可能有很多路由?
中有一些想法
您可以使用 composer autoloading
而不是 require
"register a set of routes with callbacks that are actually declared elsewhere"
来自 docs:
Each routing method described above accepts a callback routine as its final argument. This argument can be any PHP callable...
所以你可以这样做:
$routeHandler = function ($request, $response) { echo 'My very cool handler'; };
$app->get('/my-very-cool-path', $routeHandler);
但通常人们使用 类 而不是函数:
http://www.slimframework.com/docs/objects/router.html#container-resolution
我想你几乎已经理解了基本的想法。我建议多读几遍关于路由的章节。它涵盖了一切都很好。
编码愉快,如果您需要任何其他帮助,请告诉我!
作为一个微框架,Slim 不强制执行任何特定的方法。您可以找到一个现成的结构(我想到 Slim Skeleton Application)或编写您自己的结构;与其他框架不同,Slim 不会试图保护您免受 PHP.
路由定义可以像字符串数组一样简单:
<?php // routes.php
return [
'/' => ['Foo\Home', 'index'],
'/about' => ['Foo\Home', 'about'],
'/contact' => ['Foo\Contact', 'form' ],
];
... 然后在 index.php
入口点加载和处理:
$routes = require('/path/to/routes.php');
foreach ($routes as list($path, $handler)) {
$app->get($route, $handler);
}
并且您可以利用现有的 Composer 设置自动加载您的 类,方法是将适当的目录添加到 composer.json
:
{
"require": {
"slim/slim": "^3.3",
"monolog/monolog": "^1.19"
},
"autoload": {
"psr-4": {"Foo\": "./Foo/"}
}
}
从这里开始,它可以根据需要变得尽可能复杂:在 YAML 文件中定义路由,从定义的 类 自动加载等等
(代码示例仅供参考,可能无效。)
在 Slim Framework 的 documentation 上,它说
In this example application, all the routes are in index.php but in practice this can make for a rather long and unwieldy file! It’s fine to refactor your application to put routes into a different file or files, or just register a set of routes with callbacks that are actually declared elsewhere.
虽然它没有说明如何实际执行此操作。我唯一的想法是,您可以将代码拆分为多个 PHP 文件,然后在 index.php
中使用 include
或 require
来引用这些文件。
我也不确定 "register a set of routes with callbacks that are actually declared elsewhere"
是什么意思有没有人对此有任何想法,因为我想要构建的应用程序可能有很多路由?
您可以使用 composer autoloading
而不是require
"register a set of routes with callbacks that are actually declared elsewhere"
来自 docs:
Each routing method described above accepts a callback routine as its final argument. This argument can be any PHP callable...
所以你可以这样做:
$routeHandler = function ($request, $response) { echo 'My very cool handler'; };
$app->get('/my-very-cool-path', $routeHandler);
但通常人们使用 类 而不是函数: http://www.slimframework.com/docs/objects/router.html#container-resolution
我想你几乎已经理解了基本的想法。我建议多读几遍关于路由的章节。它涵盖了一切都很好。
编码愉快,如果您需要任何其他帮助,请告诉我!
作为一个微框架,Slim 不强制执行任何特定的方法。您可以找到一个现成的结构(我想到 Slim Skeleton Application)或编写您自己的结构;与其他框架不同,Slim 不会试图保护您免受 PHP.
路由定义可以像字符串数组一样简单:
<?php // routes.php
return [
'/' => ['Foo\Home', 'index'],
'/about' => ['Foo\Home', 'about'],
'/contact' => ['Foo\Contact', 'form' ],
];
... 然后在 index.php
入口点加载和处理:
$routes = require('/path/to/routes.php');
foreach ($routes as list($path, $handler)) {
$app->get($route, $handler);
}
并且您可以利用现有的 Composer 设置自动加载您的 类,方法是将适当的目录添加到 composer.json
:
{
"require": {
"slim/slim": "^3.3",
"monolog/monolog": "^1.19"
},
"autoload": {
"psr-4": {"Foo\": "./Foo/"}
}
}
从这里开始,它可以根据需要变得尽可能复杂:在 YAML 文件中定义路由,从定义的 类 自动加载等等
(代码示例仅供参考,可能无效。)