Codeigniter 3博客应用:动态变量覆盖默认静态变量失败
Hey there! Let's sort out that issue where you couldn't dynamically override your default static data while sticking to the DRY principle. Here's a straightforward solution tailored to your CodeIgniter 3.1.8 setup:
Step 1: Update Your Static_model to Support Overrides
First, modify your Static_model to accept an optional array of override values. We'll use array_merge() to let dynamic values take precedence over defaults:
class Static_model extends CI_Model { public function get_static_data($override = []) { // Default static values $default_data = [ 'site_title' => "My Blog", 'tagline' => "A simple blog application made with Codeigniter 3", 'company_name' => "My Company", 'company_email' => "company@domain.com" ]; // Merge defaults with overrides (overrides take priority) return array_merge($default_data, $override); } }
Step 2: Use the Model in Your Posts Controller
Now in your Posts controller, you can pass dynamic values directly to get_static_data() whenever you need to override defaults. This keeps your code DRY because you're reusing the base static data logic instead of rewriting it:
class Posts extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('Static_model'); // Load your Post_model here if you haven't already $this->load->model('Post_model'); } public function index() { // Override site title and tagline for the posts list page $dynamic_overrides = [ 'site_title' => "Latest Posts | My Blog", 'tagline' => "Check out our newest blog content" ]; // Get merged static data $data = $this->Static_model->get_static_data($dynamic_overrides); // Add your posts data to the array $data['posts'] = $this->Post_model->get_all_posts(); $this->load->view('posts/index', $data); } public function view($slug) { $post = $this->Post_model->get_post_by_slug($slug); if (!$post) { show_404(); } // Override site title with the post's title $dynamic_overrides = [ 'site_title' => "{$post->title} | My Blog" ]; $data = $this->Static_model->get_static_data($dynamic_overrides); $data['post'] = $post; $this->load->view('posts/view', $data); } }
Step 3: Supercharge DRY with a Base Controller (Optional)
If you have multiple controllers that need this static data logic, create a base controller to avoid repeating code across all controllers:
- Create
application/core/MY_Controller.php:
class MY_Controller extends CI_Controller { protected $static_data; public function __construct() { parent::__construct(); $this->load->model('Static_model'); // Initialize with default static data $this->static_data = $this->Static_model->get_static_data(); } // Reusable method to update static data with overrides protected function update_static_data($override = []) { $this->static_data = array_merge($this->static_data, $override); return $this->static_data; } }
- Update your
Postscontroller to inherit fromMY_Controller:
class Posts extends MY_Controller { public function __construct() { parent::__construct(); $this->load->model('Post_model'); } public function index() { $data = $this->update_static_data([ 'site_title' => "Latest Posts | My Blog", 'tagline' => "Check out our newest blog content" ]); $data['posts'] = $this->Post_model->get_all_posts(); $this->load->view('posts/index', $data); } public function view($slug) { $post = $this->Post_model->get_post_by_slug($slug); if (!$post) { show_404(); } $data = $this->update_static_data([ 'site_title' => "{$post->title} | My Blog" ]); $data['post'] = $post; $this->load->view('posts/view', $data); } }
Why This Works
The key change is adding the $override parameter to get_static_data(). array_merge() ensures any values you pass in dynamically will overwrite the default ones, while keeping all unused default values intact. This way, you're reusing the base static data logic instead of duplicating it across controller methods.
内容的提问来源于stack exchange,提问作者Razvan Zamfir

