MVC yang baik dan benar

jadi gini gan..saya disini menggunakan framework php codeigniter saya masih bingung dengan penggunaan routes,controllers dan juga model pada codeigniter.

saya disini mempunyai 2 controller(admin dan halaman)pada admin berisi controller untuk bagian admin sedangkan halaman berisi controller untuk tampilan user nahh bagaimana caranya agar controller pada admin dapat di pakai di controller user untuk memanggil data data tanpa penulisan ulang?

CONTROLLER HALAMAN :

 public function view($halaman='content'){
      if(!file_exists(APPPATH."views/pages/".$halaman.'.php')){
        show_404();
      }
      $data['judul'] = $halaman;
			$this->load->view('templates/header');
      $this->load->view('pages/'.$halaman);
      $this->load->view('templates/footer');
  }

	public function allevent($slug = null)
	{

		$row=$this->admin_model->baris();

		$this->load->library('pagination');
		$config['base_url'] = 'http://localhost/HMIFFF/event';
		$config['total_rows'] = $row;
		$config['per_page'] = 8;
		$config['full_tag_open'] = "<ul class='pagination'>";
	$config['full_tag_close'] = '</ul>';
	$config['num_tag_open'] = '<li>';
	$config['num_tag_close'] = '</li>';
	$config['cur_tag_open'] = '<li class="active"><a href="#">';
	$config['cur_tag_close'] = '</a></li>';
	$config['prev_tag_open'] = '<li>';
	$config['prev_tag_close'] = '</li>';
	$config['first_tag_open'] = '<li>';
	$config['first_tag_close'] = '</li>';
	$config['last_tag_open'] = '<li>';
	$config['last_tag_close'] = '</li>';
	$config['prev_link'] = '<i class="fa fa-long-arrow-left"></i>Previous Page';
	$config['prev_tag_open'] = '<li>';
	$config['prev_tag_close'] = '</li>';
	$config['next_link'] = 'Next Page<i class="fa fa-long-arrow-right"></i>';
	$config['next_tag_open'] = '<li>';
	$config['next_tag_close'] = '</li>';

		$start=$this->uri->segment(3);
		$this->pagination->initialize($config);

		$data['events']=$this->admin_model->pagination_event($config['per_page'],$start);
		// $data['events']=$this->admin_model->get_news();
// $data['event_item']=$this->admin_model->get_news($slug);

			$this->load->view('templates/header');
			$this->load->view('pages/event',$data);
			$this->load->view('templates/footer',$data);

	}

CONTROLLER ADMIN

 public function allevent($slug = null)
	{

		$row=$this->admin_model->baris();

		$this->load->library('pagination');

		$config['base_url'] = 'http://localhost/HMIFFF/admin/allevent';
		$config['total_rows'] = $row;
		$config['per_page'] = 5;
		$config['full_tag_open'] = "<ul class='pagination'>";
	$config['full_tag_close'] = '</ul>';
	$config['num_tag_open'] = '<li>';
	$config['num_tag_close'] = '</li>';
	$config['cur_tag_open'] = '<li class="active"><a href="#">';
	$config['cur_tag_close'] = '</a></li>';
	$config['prev_tag_open'] = '<li>';
	$config['prev_tag_close'] = '</li>';
	$config['first_tag_open'] = '<li>';
	$config['first_tag_close'] = '</li>';
	$config['last_tag_open'] = '<li>';
	$config['last_tag_close'] = '</li>';

	$config['prev_link'] = '<i class="fa fa-long-arrow-left"></i>Previous Page';
	$config['prev_tag_open'] = '<li>';
	$config['prev_tag_close'] = '</li>';

	$config['next_link'] = 'Next Page<i class="fa fa-long-arrow-right"></i>';
	$config['next_tag_open'] = '<li>';
	$config['next_tag_close'] = '</li>';

		$start=$this->uri->segment(3);
		$this->pagination->initialize($config);

		$data['events']=$this->admin_model->pagination_event($config['per_page'],$start);
		// $data['events']=$this->admin_model->get_news();
// $data['event_item']=$this->admin_model->get_news($slug);
		if ($this->session->has_userdata('username')){
		$this->load->view('admin/pages/static/header');
		$this->load->view('admin/pages/forms/allevent',$data);

		$this->load->view('admin/pages/static/footer');
}else{
	redirect('admin/login');

}
	}

yang saya inginkan memanggil fungsi tersebut agar dapat digunakan pada controler Halaman

tolong bantuannya agan agan :(

avatar farisdewantoro
@farisdewantoro

119 Kontribusi 16 Poin

Diperbarui 6 tahun yang lalu

3 Jawaban:

Jawaban Terpilih

Sebelumnya , mas yakin mau fungsi yang harusnya buat admin aja di panggil buat user juga ?

Mas bisa pakai inheritances apa sih bahasa indonesianya wkw

cth:

 class Admin extends CI_Controller {

    public function index() {

    }

    public function a(){

    }
}

include_once (dirname(__FILE__) . "/admin.php");

class Halaman extends Admin {

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

    public function methodB(){
        $this->a();
    }
}

Atau kalau mas tertarik mas bisa pakai HMVC yang udah tersedia mas https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/ bisa panggil controller dari controller lain, model dari model lain, view dari view lain :D

avatar markeronly
@markeronly

378 Kontribusi 230 Poin

Dipost 6 tahun yang lalu

@markeronly emang ga baik ya kalo seperti itu?

avatar farisdewantoro
@farisdewantoro

119 Kontribusi 16 Poin

Dipost 6 tahun yang lalu

coba mulai ke HMVC , memisahkan semua modul ke masing2 bagian yang berbeda contoh folder admin punya folder controller,models, dan views yang berbeda, nantinya akan mempermudah dalam pengembangan dan kerja team, jadi kalo satu anggota team bikin error ga berdampak ke modul lain.

https://github.com/alzen8work/CodeIgniter-HMVC

avatar Saputroandhi
@Saputroandhi

509 Kontribusi 162 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban