0

私はおっとを学んでおり、途中でphp標準のPSR-0とPSR-1を実装しようとしています。Swiftlet に基づく小さな MVC フレームワークを作成することから始めました。

URLから呼び出しているコントローラー内のベースビューコントローラーから関数を呼び出そうとしていますが、「非オブジェクトのメンバー関数set()への呼び出し」が表示されます。

すべてのクラスが正常にロードされます。したがって、コントローラーで $this->view->set('helloWorld', 'Hello world!'); を呼び出します。しかし、私はエラーが発生します。名前空間の構造を正しく取得しようとして問題が発生したので、これが原因でしょうか?

ファイル構造は次のとおりです。

index.php

lib/bootstrap.php

lib/view.php

lib/controller.php

アプリ/コントローラー/index.php

それぞれのコードは次のとおりです。

index.php

<?php

namespace MVC;

    // Bootstrap the application
    require 'lib/Bootstrap.php';

    $app = new lib\Bootstrap;

    spl_autoload_register(array($app, 'autoload'));

    $app->run();
    $app->serve();

ブートストラップ.php

namespace MVC\lib;

class Bootstrap 
{
    protected
        $action     = 'index',
        $controller,
        $hooks      = array(),
        $view
        ;

    /**
     * Run the application
     */

    function run() 
    {
               ... Code that gets controller and the action form the url

        $this->view = new \lib\View($this, strtolower($controllerName));

        // Instantiate the controller
        $controllerName = 'app\Controllers\\' . basename($controllerName);

        $this->controller = new $controllerName();                      

        // Call the controller action
        $this->registerHook('actionBefore');

        if ( method_exists($this->controller, $this->action) ) {
            $method = new \ReflectionMethod($this->controller, $this->action);

            if ( $method->isPublic() && !$method->isFinal() && !$method->isConstructor() ) {
                $this->controller->{$this->action}();
            } else {
                $this->controller->notImplemented();
            }
        } else {
            $this->controller->notImplemented();
        }

        return array($this->view, $this->controller);

    }


<?php

namespace MVC\lib;

class Bootstrap 
{
    protected
        $action     = 'index',
        $args       = array(),
        $config     = array(),
        $controller,
        $hooks      = array(),
        $plugins    = array(),
        $rootPath   = '/',
        $singletons = array(),
        $view
        ;

    /**
     * Run the application
     */

    function run() 
    {
        // Determine the client-side path to root
        if ( !empty($_SERVER['REQUEST_URI']) ) {
            $this->rootPath = preg_replace('/(index\.php)?(\?.*)?$/', '', $_SERVER['REQUEST_URI']);

            if ( !empty($_GET['route']) ) {
                $this->rootPath = preg_replace('/' . preg_quote($_GET['route'], '/') . '$/', '', $this->rootPath);
            }
        }

        // Extract controller name, view name, action name and arguments from URL
        $controllerName = 'Index';

        if ( !empty($_GET['route']) ) {
            $this->args = explode('/', $_GET['route']);

            if ( $this->args ) {
                $controllerName = str_replace(' ', '/', ucwords(str_replace('_', ' ', str_replace('-', '', array_shift($this->args)))));
            }

            if ( $action = $this->args ? array_shift($this->args) : '' ) {
                $this->action = str_replace('-', '', $action);
            }
        }

        if ( !is_file('app/Controllers/'. $controllerName . '.php') ) {
            $controllerName = 'Error404';
        }

        $this->view = new \lib\View($this, strtolower($controllerName));

        // Instantiate the controller
        $controllerName = 'app\Controllers\\' . basename($controllerName);

        $this->controller = new $controllerName();                      

        // Call the controller action
        $this->registerHook('actionBefore');

        if ( method_exists($this->controller, $this->action) ) {
            $method = new \ReflectionMethod($this->controller, $this->action);

            if ( $method->isPublic() && !$method->isFinal() && !$method->isConstructor() ) {
                $this->controller->{$this->action}();
            } else {
                $this->controller->notImplemented();
            }
        } else {
            $this->controller->notImplemented();
        }

        $this->registerHook('actionAfter');

        return array($this->view, $this->controller);

    }

<?php

namespace MVC\lib;

class Bootstrap 
{
    protected
        $action     = 'index',
        $args       = array(),
        $config     = array(),
        $controller,
        $hooks      = array(),
        $plugins    = array(),
        $rootPath   = '/',
        $singletons = array(),
        $view
        ;

    /**
     * Run the application
     */

    function run() 
    {
        // Determine the client-side path to root
        if ( !empty($_SERVER['REQUEST_URI']) ) {
            $this->rootPath = preg_replace('/(index\.php)?(\?.*)?$/', '', $_SERVER['REQUEST_URI']);

            if ( !empty($_GET['route']) ) {
                $this->rootPath = preg_replace('/' . preg_quote($_GET['route'], '/') . '$/', '', $this->rootPath);
            }
        }

        // Extract controller name, view name, action name and arguments from URL
        $controllerName = 'Index';

        if ( !empty($_GET['route']) ) {
            $this->args = explode('/', $_GET['route']);

            if ( $this->args ) {
                $controllerName = str_replace(' ', '/', ucwords(str_replace('_', ' ', str_replace('-', '', array_shift($this->args)))));
            }

            if ( $action = $this->args ? array_shift($this->args) : '' ) {
                $this->action = str_replace('-', '', $action);
            }
        }

        if ( !is_file('app/Controllers/'. $controllerName . '.php') ) {
            $controllerName = 'Error404';
        }

        $this->view = new \lib\View($this, strtolower($controllerName));

        // Instantiate the controller
        $controllerName = 'app\Controllers\\' . basename($controllerName);

        $this->controller = new $controllerName();                      

        // Call the controller action
        $this->registerHook('actionBefore');

        if ( method_exists($this->controller, $this->action) ) {
            $method = new \ReflectionMethod($this->controller, $this->action);

            if ( $method->isPublic() && !$method->isFinal() && !$method->isConstructor() ) {
                $this->controller->{$this->action}();
            } else {
                $this->controller->notImplemented();
            }
        } else {
            $this->controller->notImplemented();
        }

        $this->registerHook('actionAfter');

        return array($this->view, $this->controller);

    }

lib/view.php

namespace lib;

class View 
{
    protected
        $app,
        $variables = array()
        ;

    public
        $name
        ;

    /**
     * Constructor
     * @param object $app
     * @param string $name
     */
    public function __construct($app, $name)
    {
        $this->app  = $app;
        $this->name = $name;
    }


    /**
     * Set a view variable
     * @param string $variable
     * @param mixed $value
     */
    public function set($variable, $value = null)
    {
        $this->variables[$variable] = $value;
    }

そして最後に app/controllers/index.php

namespace app\Controllers;

class index extends \lib\Controller

{

    public function test()

    {
           // This gets the error
                   $this->view->set('helloWorld', 'Hello world!');  
    }

}
4

1 に答える 1

0

それがコントローラー内のすべてのコードである場合、 は$this->viewオブジェクトではありません。
次のコードを実行してみてください。

namespace app\Controllers;

class index extends \lib\Controller
{
    public function test()
    {
           var_dump( $this->view );
           exit;
           $this->view->set('helloWorld', 'Hello world!');  
    }

}

また、PHP では__construct()メソッドが継承されないことも知っておく必要があります。
脳に何らかの障害を負ったに違いない。

ああ..そして、なぜこの質問にタグが付いているのかわかりません。MVC 的なものを書こうとしていますが、問題自体はアーキテクチャ パターンとしての MVC とは関係ありません。

于 2012-06-18T19:23:37.243 に答える