dongyu5104
2014-04-10 15:13
浏览 354
已采纳

CodeIgniter - 从控制器中的另一个函数加载视图

Here's the issue :

If I call www.mysite.com/quizz, it loads my 'quizz' page.

But if I call www.mysite.com/quizz/start (containing the same code), the view is messed up because of broken links (images, css, ...)

Here is the content :

public function index()
{
       $this->load->helper('quizzsettings');
       $data['questions'] = getSettings();
       $this->load->view('head'); 
       $this->load->view('quizz', $data);
}

If I call another function (www.mysite.com/quizz/start), the layout fails :

public function start()
{
       $this->load->helper('quizzsettings');
       $data['questions'] = getSettings();
       $this->load->view('head'); 
       $this->load->view('quizz', $data);
}

As you know, CodeIgniter reads the URL like www.mysite.com/class/method, so it should be good, right?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqb77047 2014-04-10 16:02
    已采纳

    You want to call your CSS, JS and images using an absolute path. I suspect you are using a relative path. If you haven't already it's best to create a common header view and use base_url(). For example

    <!-- Load CSS -->
    <link type="text/css" rel="stylesheet" href="<?php echo base_url(); ?>css/style.css" />
    
    <!-- Load JS -->
    <script type="text/javascript" src="<?php echo base_url(); ?>js/script.js"></script>
    

    Obviously change the path to the files to be correct for your installation

    打赏 评论
  • douhao7677 2014-04-10 15:29

    You can call $this->function(); in your method but it is not advised. The broken code you are speaking of, from what I understand is the HTML which is loaded fine, but the images/css etc are broken because you are not calling the files properly.

    Can you post a sample of HTML you get that is "broken" please? (from the browser, with view source or ctrl+U' orcommand+U` in 90% of the browsers)

    I advise you to review your view's html and how you load images and css files, and you should be set.

    lookup please codeigniter URL helper (it might be URI helper in the docs, don't recall) and they have a simple site_url() function that i advise you use... you can call it either by site_url('controller/method') or site_url('3rdparty/jQuery/jQuery.min.js') - nice hack that works great with both controller methods and files, and you don't have to care about where you install the software

    打赏 评论

相关推荐 更多相似问题