Fatih Akgun
Fatih Akgun

Reputation: 553

Codeigniter show error page instead Exception error

this is my code

public function movieInfo($movieId = null) {

    // get the data
    $movie = $this->repository->load($movieId);

    $this->set_title(null);

    $data = $this->includes;

    $content_data = array(
        'movie' => $movie,
    );

    $data['content'] = $this->load->view('movie/movie_info', $content_data, true);
    $this->load->view($this->template, $data);

}

when i browse to http://tmdb.instaplace.me/movie/movieinfo/76341 it shows my page

but when i browse to http://tmdb.instaplace.me/movie/movieinfo/0

it shows a uncaught exception page when error reporting is set to development otherwise it shows a blank page

instead of showing blank page or uncaught exception on development i want to show a page that says "The id does not exist" for example or a 404 page but my problem is i can't figure it out how to do that, i am using this php-tmdb wrapper

the $movieId variable is the id from themoviedb.org to find the movie based on the id

Upvotes: 0

Views: 390

Answers (1)

cartalot
cartalot

Reputation: 3148

if( !  $movie = $this->repository->load($movieId) ){

      $this->_showMovieNotFoundFor($movieId) ;  } 

else{ $this->_showFound($movie) ;  } 

edit in response to question ====

you have to create the _show methods yourself. the idea is that you are just checking if a movie came back from the database or not. in the controller the simpler you can make your methods the easier it is to maintain. and unless a method in a controller has to be public, always make them private. in codeigniter you can just put an underscore before the method name to make it private. so the code

 if( !  $movie = $this->repository->load($movieId)

so if the $movie did NOT come back from database - then go to

$this->_showMovieNotFoundFor($movieId) ;

i included $movieId in case its needed for the error message.

otherwise you got $movie from database so go to

$this->_showFound($movie) ;

where if we just paste your code its going to be something like

   function _showFound($movie){

  $this->set_title(null);

    $data = $this->includes;

    $content_data = array(
        'movie' => $movie,
    );

    $data['content'] = $this->load->view('movie/movie_info', $content_data, true);
    $this->load->view($this->template, $data);

} 

Upvotes: 1

Related Questions