相关疑难解决方法(0)

从不同的文件和目录自动加载类和函数

我有这个自动加载代码:

function __autoload($class_name)
{
    //class directories
    $directorys = array(
        './Controls/',
        './Config/',
        './Utility/'
    );
    //for each directory
    foreach($directorys as $directory)
    {
        //see if the file exsists
        if(file_exists($directory.$class_name . '.php'))
        {
            require_once($directory.$class_name . '.php');
            //only require the class once, so quit after to save effort (if you got more, then name them something else
            return;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有三个目录,他们持有我所有的类和函数.

我可以在Controls目录中创建一个自动加载文件,并使用它来加载其他php文件中的所有函数或类,我的意思是例如index.php文件/portal/main/index.php

是有可能加载是类controlsconfig在index.php文件,而不包括在上述的任意文件index.php的文件

我的意思是自动加载会自动了解哪个文件正在请求类或函数,并为其包含该文件.

更新的代码:

function __autoload($class_name)
{
    //class directories
    $directorys = array( …
Run Code Online (Sandbox Code Playgroud)

php .htaccess class include autoload

8
推荐指数
2
解决办法
1317
查看次数

php中的自动加载功能

是否可以自动加载功能?

我所拥有的是我编写的函数分布在以函数名称命名的不同文件上,因此我需要的是自动加载包含该函数的文件.有没有办法做到这一点?

php function

4
推荐指数
1
解决办法
2763
查看次数

实现辅助功能的有效方法?

我正在尝试实现一个辅助函数,它可以帮助我在 MVC 应用程序中“添加”路由(映射到控制器/操作)。

我的index.php 文件如下所示。

//  use block
use App\Router;
use App\Config;
use App\Core\Database;
use App\Bootstrap;

// dependencies
$router    = new Router;
$config    = new Config;
$database  = new Database($config);
$bootstrap = new Bootstrap($router, $database);

// routes
require '../App/Routes.php';


$bootstrap->router->dispatch($_SERVER['QUERY_STRING']);
Run Code Online (Sandbox Code Playgroud)

我的“路线”文件中有一条路线,我的路线如下所示。

$bootstrap->router->add('/home', ['controller' => 'home', 'action' => 'index']);
Run Code Online (Sandbox Code Playgroud)

我更愿意像这样简单地将根添加到我的文件中......

route('/home', ['controller' => 'home', 'action' => 'index']);
Run Code Online (Sandbox Code Playgroud)

所以我需要做一个辅助函数

问题

感谢 psr-4 自动加载,我知道如何在需要时提取类,但是当涉及到平面旧函数时,这一点不太清楚。如何将辅助函数干净地添加到 MVC 框架中,而无需到处添加 require 文件?

非常感谢您的时间和考虑!

php frameworks helper psr-4

1
推荐指数
1
解决办法
1558
查看次数

标签 统计

php ×3

.htaccess ×1

autoload ×1

class ×1

frameworks ×1

function ×1

helper ×1

include ×1

psr-4 ×1