EllisLab text mark
Advanced Search
     
If no record found, redirect to 404
Posted: 11 May 2010 06:53 AM   [ Ignore ]
Joined: 2008-08-21
250 posts

Hi,

I was wondering, how do I redirect the user if a slug is not found in the database?

In my Controller, I have:

function pages($slug){
    $data[
'page_data'$this->MPages->getPageBySlug($slug);
    
$data['title'$data['page_data']['title'];
    
$data['id'$data['page_data']['id'];
    
$data['body'$data['page_data']['body'];
    
$data['main''public_page';
    
$this->load->vars($data);
    
$this->load->view('template');  
  

This retrieves the page details based on the slug.


In my Model, I have:

function getPageBySlug($slug){
        $data 
= array();
        
$this->db->where('slug',$slug);
        
$this->db->limit(1);
        
$Q $this->db->get('webpages');
        if (
$Q->num_rows() > 0){
          $data 
$Q->row_array();
        
}
        $Q
->free_result();    
        return 
$data;    
    

Would I have an else in my Model function and then let the Controller know if no results were found?

Many thanks for any guidance.

 
Posted: 11 May 2010 06:59 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2009-10-18
334 posts
invision - 11 May 2010 10:53 AM

Hi,

I was wondering, how do I redirect the user if a slug is not found in the database?

In my Controller, I have:

function pages($slug){
    $data[
'page_data'$this->MPages->getPageBySlug($slug);
    
$data['title'$data['page_data']['title'];
    
$data['id'$data['page_data']['id'];
    
$data['body'$data['page_data']['body'];
    
$data['main''public_page';
    
$this->load->vars($data);
    
$this->load->view('template');  
  

This retrieves the page details based on the slug.


In my Model, I have:

function getPageBySlug($slug){
        $data 
= array();
        
$this->db->where('slug',$slug);
        
$this->db->limit(1);
        
$Q $this->db->get('webpages');
        if (
$Q->num_rows() > 0){
          $data 
$Q->row_array();
        
}
        $Q
->free_result();    
        return 
$data;    
    

Would I have an else in my Model function and then let the Controller know if no results were found?

Many thanks for any guidance.

I usually make my models return FALSE on failure, and let the controller ask this:

if( ! $return{
  show_404
();

To make show_404() show a custom 404 you will have to do a lot of work (there are some topics about that in this forum). Another option is redirecting like you said

 Signature 

Wallpapers and Images Site: Desktop Wallpapers

 
Posted: 11 May 2010 07:03 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Joined: 2008-08-21
250 posts

Many thanks Buso.

So perhaps something like this:

Model

function getPageBySlug($slug){
        $data 
= array();
        
$this->db->where('slug',$slug);
        
$this->db->limit(1);
        
$Q $this->db->get('webpages');
        if (
$Q->num_rows() > 0){
          $data 
$Q->row_array();
        
else {
          $data 
false;
          return 
false;
        
}
        $Q
->free_result();    
        return 
$data;    
    

Controller

function pages($slug){
    $data[
'page_data'$this->MPages->getPageBySlug($slug);

if (
$data false{ redirect('/404/'}

    $data[
'title'$data['page_data']['title'];
    
$data['id'$data['page_data']['id'];
    
$data['body'$data['page_data']['body'];
    
$data['main''public_page';
    
$this->load->vars($data);
    
$this->load->view('template');  

I’m a bit new to this so any help at all is super appreciated.

 

Thank you.

 
Posted: 11 May 2010 07:13 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2009-10-18
334 posts

be careful with assignations and comparations

if($data = false) will set it to false

if($data == false) will work, but using === is considered a good practice, so you don’t have things like 0 evaluating to false

 Signature 

Wallpapers and Images Site: Desktop Wallpapers

 
Posted: 11 May 2010 07:25 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2008-08-21
250 posts

Many thanks Buso. I will try this shortly. I’m getting an unrelated Database Error right now. Nice smile

 
Posted: 11 May 2010 07:36 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Joined: 2008-08-21
250 posts

Only issue now is that it doesn’t seem to be redirecting the user.

It just the template, without any of the variables in place.

In my pages function in the Controller, I’m using:

if ($data === false
      redirect
('/index.php/page/aboutus/','refresh'); 
 
Posted: 11 May 2010 08:01 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-11
2985 posts
invision - 11 May 2010 11:36 AM

Only issue now is that it doesn’t seem to be redirecting the user.

It just the template, without any of the variables in place.

In my pages function in the Controller, I’m using:

if ($data === false
      redirect
('/index.php/page/aboutus/','refresh'); 

That code block should be:

$data or redirect('page/aboutus'); 

Never add index.php and you don’t really need to add preceding or trailing slashes.

 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

 
Posted: 11 May 2010 08:31 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Joined: 2008-08-21
250 posts

Hi Phil,

Many thanks for your input.

Just to confirm this pages function is part of my Page controller:

function pages($slug){
    $data[
'page_data'$this->MPages->getPageBySlug($slug);
    
    
$data or redirect('page/aboutus');
    
    
$data['title'$data['page_data']['title'];
    
$data['id'$data['page_data']['id'];
    
$data['body'$data['page_data']['body'];
    
$data['main''public_page';
    
$this->load->vars($data);
    
$this->load->view('template');  
  

It’s still not redirecting. Is it because of the line above the redirect?


The rest of my Page Controller is as follows:

<?php
class Page extends Controller {
  
function Page(){
    parent
::Controller();
    
session_start();
  
}

  
function index(){
    $data[
'page_data'$this->MPages->getPageBySlug('home');
    
$data['cats'$this->MPages->getPages();
    
$data['title'$data['page_data']['title'];
    
$data['main''public_home';
    
$this->load->vars($data);
    
$this->load->view('template');  
  
}
  
  
function aboutus($page="aboutus"{
    $this
->pages($page);
  
}
  
  
function services($page="services"{
    $this
->pages($page);
  
}
  } 


Thanks again for your help.

 
Posted: 11 May 2010 08:35 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-11
2985 posts
invision - 11 May 2010 12:31 PM

It’s still not redirecting. Is it because of the line above the redirect?

Of course!

You are testing that $data is empty after populating it. Even if this model fails you will have:

$data['page_data'false// or array() 

That is a full variable.

So…

$data['page_data'or redirect('page/aboutus'); 
 Signature 

————————
Blog | Twitter | GitHub | BitBucket
————————-
PyroCMS - open source modular CMS built with CodeIgniter
PancakeApp - Simple, hosted invoicing/w project management

 
Posted: 11 May 2010 08:39 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2008-08-21
250 posts

Apologies again for these simple queries.

function pages($slug){
    $data[
'page_data'$this->MPages->getPageBySlug($slug);
    
$data['page_data'or redirect('page/aboutus');
    
    
$data['title'$data['page_data']['title'];
    
$data['id'$data['page_data']['id'];
    
$data['body'$data['page_data']['body'];
    
$data['main''public_page';
    
$this->load->vars($data);
    
$this->load->view('template');  
  

Is this correct?

 
Posted: 11 May 2010 08:40 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2008-08-21
250 posts

Whoop! It is smile


Thanks again for your help and patience Phil.