Codeigniter - 重定向 [英] Codeigniter - redirect
问题描述
我有从 index
重定向到 logotyp
或 tiskoviny
。当我写浏览器( localhost / my_page / sluzby / logotyp
)它工作,但它显示没有CSS(这是第二个问题)。 URL Helper
是自动加载的。
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>
感谢您的帮助。
推荐答案
如前所述,请确保在开始之前没有空格(空格,制表符,新行等)<?php
在您的控制器。如果您有一个关闭?>
标记,那么这可以删除,因为它可能会导致 redirect
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
.
在这种情况下,如果您不关心在URL中包含函数的名称在URL中显示您正在调用的函数的URL( logotyp()
),而 redirect
)/刷新页面,那么您可以直接在 index()
函数中调用相关函数:
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();
}
CSS不加载);它可能发生,因为到其路径的链接不正确。使用CodeIgniter的 URL Helper
中的 base_url
函数可以轻松生成正确的路径。我不知道你的CSS文件位于哪里,但对于这个例子,我将假设,相对于网站的根,它在这里: assets / css / style.css
只需添加相对于web根目录的路径:
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">
HTML助手中的html_helper.html#link_tagrel =nofollow> link_tag()也可以帮助生成样式表链接。
link_tag() in the HTML Helper
can also help with the generation of stylesheet links.
这篇关于Codeigniter - 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!