Welcome Guest, Not a member yet? Register   Sign In
URI - Routing
#1

Hi, 

for example let's say we have a static page: site.com/contact
and our Controller should look like:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Contact extends API_Controller {
    
    public function 
__construct() {
        
parent::__construct();
    }
    
    public function 
index(){
        
$this->load->view("contact-us");
    }

but now we want a URL to contact form to be like: site.com/contact-us
in order to do that we will put in our config/routes.php
PHP Code:
$route['contact-us'] = 'contact/index'
and this works.

But the problem here is that a user can access the contact page from 2 url-s:
site.com/contact
and
site.com/contact-us

But, we want access to contact form from only one from this url: site.com/contact-us
Can we set up somewhere that only routes specified in config/routes.php are 'valid' and if user access  a contact form via site.com/contact to see 404 page?
 

At them moment I am doing things like this in controller:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Contact extends CI_Controller {
    
    public function 
__construct() {
        
parent::__construct();
    }
    
    public function 
index(){
        if(
$this->uri->segment(1) == "contact-us"){
            
$this->load->view("contact-us");
        }
        else{
            
show_404();
        }
    }

which is very exhausting.

Thanks
Reply
#2

Why not removing the index()

PHP Code:
$route['contact-us'] = 'contact/show_ContactUs'

And your controller

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Contact extends API_Controller {
    
    public function 
__construct() {
        
parent::__construct();
    }
    
    public function 
show_ContactUs(){
        
$this->load->view("contact-us");
    }

Reply




Theme © iAndrew 2016 - Forum software by © MyBB