Ich bin ein sehr neuer Neuling in CodeIgniter, und während ich fortfahre, stoße ich auf Probleme, die in der Verfahrenscodierung leicht zu beheben waren
Das aktuelle Thema ist: Ich habe diesen Controller
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);
}
}
Wie Sie sehen, wiederholen sich einige Array-Elemente immer wieder:
$data['title'] = 'Page Title';
$data['robots'] = 'noindex,nofollow';
$data['css'] = $this->config->item('css');
Gibt es keine Möglichkeit, sie im Controller "global" zu machen, so dass ich sie nicht für jede Funktion eingeben muss? Etwas wie (aber das gibt mir einen Fehler):
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 im Voraus!
Sie können "Klassenvariablen" erstellen, auf die von jeder Methode im Controller aus zugegriffen werden kann. Im Konstruktor legen Sie diese Werte fest.
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);
}
}
Sie können eine Klasseneigenschaft namens data einrichten und dann ihre Standardwerte im Konstruktor festlegen. Dies ist der erste Vorgang, der ausgeführt wird, wenn eine neue Instanz auf Basic
erstellt wird .. __ Sie können darauf mit dem Schlüsselwort $this
referenzieren
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);
}
}
hey, danke, hier ist mein Snipet. Es ist eine globale Variable, die eine Sicht hat
/* 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');
}
}
Obwohl es schon so lange her ist. Es kann hilfreich sein, wenn Sie $ this-> load-> vars ($ data) verwenden können. in core MY_controller, um $ data array in allen Ansichten verfügbar zu machen.
/* 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');
}
}
Warum nicht einen Helfer benutzen?
Datei:
/application/helpers/meta_helper.php
Inhalt:
<?php
function meta_data() {
return array("title" => null, "robots" => "noindex, nofollow" );
}
In Ihrem Controller:
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);
}
Und in deiner Ansichtsvorlage:
<title><?php $meta['title']; ?></title>
Wird ausgegeben:
<title>My Specific Page Title</title>
Hoffe das macht Sinn :-)!