25

私はCodeIgniterの初心者ですが、続行していると、手続き型コーディングで簡単に修正できる問題が発生します。

現在の問題は次のとおりです:私はこのコントローラーを持っています

class Basic extends Controller {

    function index(){
        $data['title'] = 'Page Title';
        $data['robots'] = 'noindex,nofollow';
        $data['css'] = $this->config->item('css');
        $data['my_data'] = 'Some chunk of text';
        $this->load->view('basic_view', $data);
    }

    function form(){
        $data['title'] = 'Page Title';
        $data['robots'] = 'noindex,nofollow';
        $data['css'] = $this->config->item('css');
        $data['my_other_data'] = 'Another chunk of text';
        $this->load->view('form_view', $data);
    }
}

ご覧のとおり、一部の配列項目は何度も繰り返されます。

$data['title'] = 'Page Title';
$data['robots'] = 'noindex,nofollow';
$data['css'] = $this->config->item('css');

それらをコントローラーで「グローバル」にして、関数ごとに入力する必要がないようにする方法はありませんか?のようなもの(しかし、これは私にエラーを与えます):

class Basic extends Controller {

    // "global" items in the $data array
    $data['title'] = 'Page Title';
    $data['robots'] = 'noindex,nofollow';
    $data['css'] = $this->config->item('css');

    function index(){
        $data['my_data'] = 'Some chunk of text';
        $this->load->view('basic_view', $data);
    }

    function form(){
        $data['my_other_data'] = 'Another chunk of text';
        $this->load->view('form_view', $data);
    }

}

事前にThnaks!

4

5 に答える 5

41

できることは、コントローラーの任意のメソッドからアクセスできる「クラス変数」を作成することです。コンストラクターでは、これらの値を設定します。

class Basic extends Controller {
    // "global" items
    var $data;

    function __construct(){
        parent::__construct(); // needed when adding a constructor to a controller
        $this->data = array(
            'title' => 'Page Title',
            'robots' => 'noindex,nofollow',
            'css' => $this->config->item('css')
        );
        // $this->data can be accessed from anywhere in the controller.
    }    

    function index(){
        $data = $this->data;
        $data['my_data'] = 'Some chunk of text';
        $this->load->view('basic_view', $data);
    }

    function form(){
        $data = $this->data;
        $data['my_other_data'] = 'Another chunk of text';
        $this->load->view('form_view', $data);
    }

}
于 2012-05-14T17:32:11.910 に答える
18

dataという名前のクラスプロパティを設定し、そのデフォルト値をコンストラクターに設定できます。これは、上の新しいインスタンスBasicが作成されたときに最初に実行されるものです。$this次に、キーワードでそれを参照できます

class Basic extends Controller
{
   var $data = array();

   public function __construct()
   {
       parent::__construct();
       // load config file if not autoloaded
       $this->data['title'] = 'Page Title';
       $this->data['robots'] = 'noindex,nofollow';
       $this->data['css'] = $this->config->item('css');
   }

   function index()
   {
       $this->data['my_data'] = 'Some chunk of text';
       $this->load->view('basic_view', $this->data);
   }

   function form()
   {
       $this->data['my_data'] = 'Another chunk of text';
       $this->load->view('form_view', $this->data);
   }
}
于 2012-05-14T17:31:16.007 に答える
6

ちょっとありがとうここに私のスニペットがありますそれはビューを保持するグローバル変数です

/* Location: ./application/core/MY_Controller  */

class MY_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->data = array(
            'sidebar' => $this->load->view('sidebar', '' , TRUE),
        );
    }

}

/* Location: ./application/controllers/start.php */
class Start extends MY_Controller {

    function __construct()
    {       
        parent::__construct();
    }

    public function index()
    {
        $data = $this->data;

        $this->load->view('header');
        $this->load->view('start', $data);
        $this->load->view('footer');
    }
}
于 2013-02-11T13:16:34.273 に答える
2

長いのに。$ this-> load-> vars($ data);を使用できると他の人に役立つ場合があります。コアMY_controllerで、$data配列をすべてのビューで使用できるようにします。

/* Location: ./application/core/MY_Controller  */

class MY_Controller extends CI_Controller {

function __construct()
{
    parent::__construct();
    $data['title'] = 'Page Title';
    $data['robots'] = 'noindex,nofollow';
    $data['css'] = $this->config->item('css');
    $this->load->vars($data);
}

}

 /* Location: ./application/controllers/start.php */
class Start extends MY_Controller {

function __construct()
{       
    parent::__construct();
}

public function index()
{
    $data['myvar'] = "mystring";

    $this->load->view('header');
    $this->load->view('start', $data);
    $this->load->view('footer');
}
 }
于 2016-11-15T10:29:23.243 に答える
0

ヘルパーを使ってみませんか?

ファイル:

/application/helpers/meta_helper.php

コンテンツ:

<?php 
function meta_data() {
return array("title" => null, "robots" => "noindex, nofollow" );
}

コントローラ内:

class Basic extends Controller {

    function __construct(){
        parent::__construct();
        $this->load->helper('meta');
    }    

    function index(){
        $data['meta'] = meta_data(); //associate the array on it's own key;

        //if you want to assign specific value
        $data['meta']['title'] = 'My Specific Page Title';

        //all other values will be assigned from the helper automatically

        $this->load->view('basic_view', $data);
    }

そしてあなたのビューテンプレートで:

 <title><?php $meta['title']; ?></title>

出力します:

<title>My Specific Page Title</title>

それが理にかなっていることを願っています:-)!

于 2016-05-16T17:42:12.453 に答える