1

I have problem redirecting from index to the functions logotyp or tiskoviny. When I write to the browser (localhost/my_page/sluzby/logotyp) it works but it show without CSS (this is the second problem). The URL Helper is auto-loaded.

class Sluzby extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('uri');
        $this->load->model('sluzby_model');
        $this->load->model('pages');
    }

    public function index()
    {
        redirect('sluzby/logotyp');
    }

    public function logotyp()
    {
        $data['nadpis'] = "SomeText";
        $this->basic_template->view('basic', 'sluzby_view',$data);
    }

    public function tiskoviny()
    {
        $data['nadpis'] = "SomeText";
        $this->basic_template->view('basic', 'sluzby_view',$data);
    }
}

The browser shows me this error when I want to redirect from index:

<p>Severity: Warning<br>
Message:  Cannot modify header information - headers already sent by (output started at C:\Program Files\VertrigoServ\www\dsvision\application\controllers\sluzby.php:1)<br>
Filename: helpers/url_helper.php<br>
Line Number: 542</p>

Thanks for help.

jleft
  • 3,457
  • 1
  • 23
  • 35
Daniel
  • 350
  • 4
  • 12

2 Answers2

2

As already mentioned, make sure there is no white space (spaces, tabs, new lines etc.) before the opening <?php tag in your controller. If you have a closing ?> tag, then this can be removed, as it can often cause issues with redirect.


In this case, if you aren't concerned about having the function's name in the URL (this will not change the URL to show the function that you are calling (logotyp()) in the URL, whereas the redirect function would)/refreshing the page, then you could just call the relevant function directly in your index() function:

public function index()
{
    $this->logotyp();
}

For your second issue (CSS not loading); it is probably occuring because the link to its path is incorrect. Using the base_url function from CodeIgniter's URL Helper you can easily generate the correct path. I don't know where your CSS file(s) is located, but for this example I'll assume that, relative to the site root, it is here: assets/css/style.css. Simply add the path relative to web root as so:

<link href="<?php echo base_url("assets/css/style.css"); ?>" type="text/css" rel="stylesheet">

link_tag() in the HTML Helpercan also help with the generation of stylesheet links.

jleft
  • 3,457
  • 1
  • 23
  • 35
  • Thanks for help. Your index example is better and it is what I want. When I use your index example, css works well but when I go to "sluzby/logotyp" it dont work. – Daniel Apr 09 '13 at 17:31
  • No problem! What does the `link` to your CSS look like in the `view` file that you load? – jleft Apr 09 '13 at 17:36
  • You need to `echo` `base_url()` and you're missing the `;` after it too. Try to replace your `href="..."` with this: `href=""` – jleft Apr 09 '13 at 17:48
  • I try it but it also dont works. – Daniel Apr 09 '13 at 17:56
  • If you load the web page in your browser and _view source_, what does the CSS link look like? – jleft Apr 09 '13 at 17:58
  • It show only ` ` – Daniel Apr 09 '13 at 18:15
  • Have you set the `base_url` value in `application/config/config.php`? – jleft Apr 09 '13 at 18:24
  • Yes, I have and still doesnt work. – Daniel Apr 09 '13 at 18:38
  • I dont know but it works now. Thank ou very much for your help. – Daniel Apr 09 '13 at 18:47
  • No problem, I'm glad it's working. If you've found an answer useful (whether it's mine or someone else's), please could you consider [accepting an answer](http://stackoverflow.com/faq#howtoask) – jleft Apr 09 '13 at 18:54
  • Nice answer +1, just one thing: when you use `$this->logotyp();`, the URL in the browser *won't* change (for the good and for the bad of it), don't forget that. – Shomz Apr 09 '13 at 20:44
  • Thanks, @Shomz! Yeah, you're totally correct. I did try to at least allude to that when I said: _'if you aren't concerned about having the function's name in the URL...'_ ; but I probably should have made that point more transparent. – jleft Apr 09 '13 at 21:24
  • I've edited my answer to further explain the URL situation. – jleft Apr 09 '13 at 21:27
1

Seems like you have some blank space in your controller... Make sure you remove it and that the file begins with <?php. You also can't output anything before the redirect (no views, no echos)...

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Thanks for help. It was really BOMs in files. – Daniel Apr 09 '13 at 17:07
  • No problem, didn't realize you were using UTF-8. And about the non-working CSS, just make sure you use absolute paths (`base_url("path_to_css");` might help). – Shomz Apr 09 '13 at 20:42