10

I was too sleepy when I asked the question, so sorry for that, anyway to make things clear I prepared the question for 2 hours.

I'm trying to organize my code and decided to organize it mvc'ish(mvc-like), I don't know if I can follow all the principles, but I wanted to be at least close to that.

My application has a front-controller (dunno if my definition is right), so that all the http-request of my application will be passing through a single point, in my case the index.php in the root directory of my application.

Having said that I have set it up like that, you can imagine that I used .htaccess to direct all request to index.php.

I exploded the url and created an array out of it, $url[] like so. So whenever I access my app like this http://localhost/app/pagename it'll be accessing a controller (pagename_controller)

I did it like this :

$file = $controller_path . $page . '_controller.php';

if (file_exists($file)) {
    require $file;
    $class_name = ucfirst($page) . '_controller';
    $target = new $class_name();
}

also I wrap it up in a Container, the 'decorator pattern', for future use, validations maybe. like this :

$controller = new Wrap($target);
$controller->index();

I don't know if the use of $controller variable name is appropriate so please forgive me when it is all wrong.

I kinda think that I can setup my application like this :

  1. user sends a request, how? by using the application means that he/she sends out a http-request, that will load the initial state of the application

    credit : phparchitect design patterns

As you can see in the diagram of my desired application structure, I was able to do only the first part which is to direct the request to a single entry (index.php)

Now the problems are the initialization of other parts of the application.

As of this moment, I have 3 files that I want to setup, but I am confused on how.

index_controller, index_view, Template

class Index_controller {
    private $model;
    private $view;

    public function __construct(){
        // optional model -> $this->model = 'index' 
        $this->view = 'index'  // 
    }

    public function index(){
        $this->load->view($this->view)
    }
}

class Index_view {
    private $model;
    private $template;

    public function __construct(Model $model = null){
         $this->template = new Template('default');
    }

    public function view() {
         $this->template->assign('css', 'default_css'); // don't know if this is efficient
         // or $this->template->assign('header', 'default_header');
         // or $this->template->assign('sidebar', 'default_sidebar');
         // or $this->template->assign('footer', 'default_footer');
         // or any other things I want to use in the template
    }
}

class Template {

    public $data = array();
    private $tmpl;

    public function __construct($template) {
         $this->tmpl = $template . '_tmpl.php';
    }

    public function assign($name, $value){
        $this->data[$name] = $value;
    }

    // public function output
    // function that will explode the data array and render it out as a webpage
    // I'll create templates and
}

With that at hand, I want to know now how do I link those things together. At the moment I have a system folder that can contain classes, and I setup a autoloader for that folder.

I am thinking of creating a Controller class and View class that acts as the ActionFactory and ViewFactory as illustrated in the diagram, although I know that these are not their responsibilities.

I am thinking of this :

class Controller {

    protected $load;
    public function __construct() {
        $this->load = new View();
    }
}

class View {
    public function __construct() {
    // some things i don't know
    }

    public function view() {
    // some things i don't know
    }
}

What are your suggestions and comments in my setup. How can I initiate the triad?

4

1 に答える 1

1

まあ、実装の詳細にこだわりすぎないようにしましょう。フレームワークの保護については、別の機会に読むことができます。あなたの質問に対処しましょう...

あなたが実装しようとしている線に沿って機能するフレームワークを実際に作成しました。あなたが欠けているのは RoutingHandler クラスだと思います。ルーティングは URL の物理的な操作であり、どのコントローラーをロードし、どのアクションを実行するかをアプリケーションに指示します。

私の世界にはモジュールもあるので、基本的なルーティングスキームは

Module -> Controller -> Action

これら 3 つの項目は、この方法で URI スキームにマップされます。変数も同様に追加できます...

http://www.domain.com/module/controller/action/var1/val1/var2/val2

では、URI が解析され、制御が適切なコントローラーとアクションに渡された後はどうなるでしょうか。簡単な例を示すためにいくつかのコードを作成しましょう...

<?php    
    class indexController extends Controller {

        protected function Initialize() {
            $this->objHomeModel = new HomeModel;

            $this->objHeader = new Header();
            $this->objFooter = new Footer();

            $this->objHeader
                ->SetPageId('home');
        }

        public function indexAction() {
            $this->objHeader->SetPageTitle('This is my page title.');
        }
    }
?>

このInitializeメソッドでは、コントローラー全体のものを設定し、後で使用するためにモデルのインスタンスを取得しています。本当の肉はindexAction方法にあります。これは、ビューで使用するものをセットアップする場所です。例えば...

public function randomAction() {
    $this->_CONTROL->Append($intSomeVar, 42);
}

_CONTROL操作してビューに渡す値の配列です。このControllerクラスは、ビューの適切なテンプレートを見つける方法を知っています。これは、アクションにちなんで (および兄弟ディレクトリにある) 名前が付けられているためです。

親クラスはアクション メソッドのController名前を取得し、次のように解析します...

indexAction -> index.tpl.php

ここでは、他の楽しいこともできます。たとえば...

Application::SetNoRender();

...コントローラーにテンプレート内でレンダリングしないように指示しますが、メソッドを完了するだけです。これは、実際には何も出力したくない場合に役立ちます。

最後に、すべてのコントローラー、モデル、およびビューは、次のように独自のディレクトリ内に存在します...

my_module
    controllers
        indexController.class.php
        someotherController.class.php
        :
        :
    models
        HomeModel.class.php
        :
        :
    templates
        index.tpl.php
        someother.tpl.php
        :
        :

続けることもできますが、私はこれを記憶から書いています。あちこちにいくつかのしわがありますが、うまくいけば、これがあなたの思考の糧になることを願っています.

于 2012-12-19T21:54:28.017 に答える