Welcome Guest, Not a member yet? Register   Sign In
Check if controller exist
#1

[eluser]Roey[/eluser]
Hello everyone...
I wander,
Is there any way that i can check if controller exist and if not then go to another controller?
#2

[eluser]LuckyFella73[/eluser]
As far as I know you can only check for classes (that's what controller are)
that are loaded.
What you can do is set up rules in config/routes.php that point to somewhere in case
a controller is called that don't exist.
#3

[eluser]Roey[/eluser]
ok...

1. where in the config file i can do this?


2. maybe if i tell you what i'm trying to do you can give another way to do it.

I try to built a small cms with an option for the admin to create a new page.
the page that he will create will be save in the view folder.

what i think about is to build a controller which will display the page without creating a new controller.

this is the controller:
Code:
<?php
class Pages extends Controller{
    
    function index($page){
        
        $data['main_content'] = $page;
        $this->load->view('includes/template', $data);
        
    }
}

and i get the $page from the address bar.

is there any other way to do so?
#4

[eluser]LuckyFella73[/eluser]
I assume that $page is the name of the view_file that should be loaded?
In that case you can check with the php function
Code:
file_exists()

Have a look here:
http://php.net/manual/de/function.file-exists.php

Check if the view file can be found in your view directory and if not
let the page display an existing one that makes sense for you.
#5

[eluser]pickupman[/eluser]
Most common CMS will store page data in the database. Here is how I am doing this same thing
Code:
class Pages extends CI_Controller{

   public function __construct(){
      parent::__construct();
      $this->load->model('pages_model');
   }
    
    function index($page = 'home'){
        
        //Get page from DB
        $data['main_content'] = $this->pages_model->get_page($page);
        
        if($data['main_content'] == FALSE){
            //Display not found page
            $data['main_content'] = $this->load->view('page_not_found', $data, TRUE);  //Save the contents of the view
        }    

        $this->load->view('includes/template', $data);
        
    }
}

//In routes.php
$route['pages/(:any)'] = 'pages/index/$1';
$route['pages']        = 'pages/index/home'; //Load home page if no segment is passed

Now using these routes your links would be pages/home, pages/about_us, pages/services etc.




Theme © iAndrew 2016 - Forum software by © MyBB