PHP7未捕获的错误:方法名称必须是字符串

时间:2018-06-30 11:28:55

标签: php frameworks

在尝试开发框架(以缩短开发时间)时,我遇到了这个问题致命错误:未捕获的错误:方法名称必须是C:\ xampp \ htdocs \ mywebsites \ gbenga \ classes中的字符串\ General \ EntryPoint.php:47堆栈跟踪:#0 C:\ xampp \ htdocs \ mywebsites \ gbenga \ public \ index.php(8):General \ EntryPoint-> run()#1 {main}抛出了C: \ xampp \ htdocs \ mywebsites \ gbenga \ classes \ General \ EntryPoint.php在第47行上,即使在stackover上,我也找不到适合我的解决方案。

这是EntryPoint.php

<?php
namespace Ninja;

class EntryPoint {
    private $route;
    private $method;
    private $routes;

    public function __construct(string $route, string $method, \Ninja\Routes $routes) {
        $this->route = $route;
        $this->routes = $routes;
        $this->method = $method;
        $this->checkUrl();
    }

    private function checkUrl() {
        if ($this->route !== strtolower($this->route)) {
            http_response_code(301);
            header('location: ' . strtolower($this->route));
        }
    }

    private function loadTemplate($templateFileName, $variables = []) {
        extract($variables);

        ob_start();
        include  __DIR__ . '/../../templates/' . $templateFileName;

        return ob_get_clean();
    }

    public function run() {

        $routes = $this->routes->getRoutes();   

        $authentication = $this->routes->getAuthentication();

        if (isset($routes[$this->route]['login']) && !$authentication->isLoggedIn()) {
            header('location: /login/error');
        }
        else if (isset($routes[$this->route]['permissions']) && !$this->routes->checkPermission($routes[$this->route]['permissions'])) {
            header('location: /login/permissionserror');    
        }
        else {
            $controller = isset($routes[$this->route][$this->method]['controller']);
            $action = !empty($routes[$this->route][$this->method]['action']);
            $page = $controller->$action();

            $title = $page['title'];

            if (isset($page['variables'])) {
                $output = $this->loadTemplate($page['template'], $page['variables']);
            }
            else {
                $output = $this->loadTemplate($page['template']);
            }

            echo $this->loadTemplate('layout.html.php', ['loggedIn' => $authentication->isLoggedIn(),
                                                         'output' => $output,
                                                         'title' => $title
                                                        ]);

        }

    }
}

这是索引页

<?php
try {
    include __DIR__ . '/../includes/autoload.php';

    $route = ltrim(strtok($_SERVER['REQUEST_URI'], '?'), '/');

    $entryPoint = new \Ninja\EntryPoint($route, $_SERVER['REQUEST_METHOD'], new \Ijdb\IjdbRoutes());
    $entryPoint->run();
}
catch (PDOException $e) {
    $title = 'An error has occurred';

    $output = 'Database error: ' . $e->getMessage() . ' in ' .
    $e->getFile() . ':' . $e->getLine();

    include  __DIR__ . '/../templates/layout.html.php';
}

我认为问题出在哪里,这行是在run方法内部

$page = $controller->$action();

我不知道代码有什么问题

1 个答案:

答案 0 :(得分:0)

根据您的代码,$action的值是!empty(...)检查的结果,因此是布尔值。