Welcome Guest, Not a member yet? Register   Sign In
confusion with urls
#1

[eluser]leet_2k[/eluser]
Hi guys,
I have just started up on codeigniter and am bit confused about urls.
I have been using relative urls in all my links till now.For eg:
<link href="css/common/css-reset.css" rel="stylesheet" type="text/css" media="all" />

I have doing the same with CI too N it seemed to work until now.
My problem is as follows:

I have created a header.php and footer.php and put them in includes folder under views
That is:
Views-> includes -> header.php & footer.php (-> indicates that they are inside a folder)

In my header.php, I have the followed code(Notice How I used relative URLs):

Code:
<?php echo $title; ?></title>
[removed][removed]
<link href="css/common/css-reset.css" rel="stylesheet" type="text/css" media="all" />
<link href="css/common/core.css" rel="stylesheet" type="text/css" media="all" />
<?php if(isset($css_specific)) : ?>
<link href="<?php echo $css_specific; ?>" rel="stylesheet" type="text/css" media="all" />
<?php endif; ?>
[removed][removed]
<?php if(isset($js)) : ?>
[removed][removed]
<?php endif; ?>
[removed][removed]

In a nutshell, I load this header.php and footer.php from template.php (code is below)

Code:
<?php $this->load->view('includes/header'); ?>
<?php
//Checking if the current page has specific css and jss and loading that

    $this->load->view($main_content,$css_specific,$js);
$this->load->view('includes/footer'); ?>

From my controllers, I pass in the $css_specific and $title variables. So for example, if I want to create a contact us page,

Code:
class Contact extends Controller{
    function __construct()
    {
        parent::Controller();
    }
    
    function index(){
        $data['title'] = 'Contact Us';
        $data['main_content'] = 'contact_view';
        $data['css_specific']='css/contact.css';
        $this->load->view('includes/template',$data);
    }
    
    function validate(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('email', 'Email address', 'trim|required|valid_email');
        $this->form_validation->set_rules('subject', 'Subject', 'trim|required');
        $this->form_validation->set_rules('message', 'Message', 'trim|require|min_length[3]|max_length[500]');
    
        if(($this->form_validation->run()) == FALSE)
        {
             // On failure

*******************************************
BIT CONFUSED ON WHAT TO DO HERE. SEE BELOW
*******************************************
          
        }
        else
        {
            //validation has passed
            $name = $this->input->post('name');
            $email = $this->input->post('email');
            $subject = $this->input->post('subject');
            $message = $this->input->post('message');
            $config = '';
        
            $this->load->library('email');
            $this->email->set_newline("\r\n");
            
            $this->email->from($email,$name);
            $this->email->to('[email protected]');
            $this->email->subject($subject);
            $this->email->message($message);
            
            if ($this->email->send())
            {
                echo 'Your message has been sent!';
            }

        }
    }
}


Now when the validation fails and I load the index controller (by using - $this->index();
), the URLs seem to be screwed up. The css and the js files in the header files do not have proper URLs.
I have also tried redirecting the page to '/contact' but then the form resets to default text which I don't want

SOLUTION POSSIBLE:

1) I have changed my css url(the css of contact page - that is $css_specific NOT header.php) to ABSOLUTE and it works. So,I guess if I change all the urls to ABSOLUTE,it'll work.

Is there ANY other way?
#2

[eluser]cahva[/eluser]
Use URL helper. You'll be using it a lot so put it to autoload. Change your view code's urls to this:
Code:
<link href="<?php echo base_url() ?>css/common/css-reset.css" rel="stylesheet" type="text/css" media="all" />
It will print the base_url you have defined in the config.
#3

[eluser]leet_2k[/eluser]
Hi cahva. Thanks for replying.
As to your suggestion, isn't there a easier way. I mean, I'll have to change lots of urls...
#4

[eluser]danmontgomery[/eluser]
Try using a <base> tag




Theme © iAndrew 2016 - Forum software by © MyBB