Codeigniter - redirect [duplicate]

怎甘沉沦 提交于 2019-12-02 11:55:03

问题


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.


回答1:


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.




回答2:


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)...



来源:https://stackoverflow.com/questions/15906877/codeigniter-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!