doushangxianq07480 2012-09-03 17:00
浏览 61
已采纳

从Codeigniter Template Partials读取数据时出现问题,以及使用控制器重定向

I have two unrelated Codeigniter problems:

(a) I am trying to redirect a user to the dashboard after logging in successfully. i.e. from controller "auth" to controller "dashboard". For some odd reason, it keeps redirecting to auth/dashboard (404 error since it does not exist).

This is what I did:

//Within the auth controller    
    if ($this->auth->login($username, $password)) //Auth->login returns boolean
    {
    redirect('/dashboard', 'refresh');
    # redirect('/dashboard', 'location');  // I tried this too
    # redirect('/dashboard/index', 'refresh');   // I also tried this!
    }

What am I doing wrong, please? It just keeps redirecting to auth/dashboard

(b) I am using Phil Sturgeon's brilliant Codeigniter Template library (without a Parser) and I am unable to receive data sent into a partials file. This data is ONLY needed by the partials file. I have been over the (unfortunately sparse) library documentation, S/O posts on the topic, and also the CI forums to no avail, there seems to be no clear-cut example showing how a partial receives data that a controller assigns to it.

This is how I have been doing it. Please tell me what I must be doing wrong.

// In the controller page
$this->data['user'] = array('info' => 'username', 'value' => 'Cogicero');
$this->data['prefs'] = array('foo' => 'bar');
$this->template
     ->set_partial('header', 'partials/header', $this->data)
     ->set_layout('blog')
     ->build('foobar_view');

And

        //In the header partials page
        <?php
        /* snip */
        print_r($data);
        print_r($prefs);
        print_r($user);
        echo $user["info"];
        echo $prefs["foo"];
        ?>

All the above produce "array does not exist" or "undefined variable" errors! How am I meant to be receiving data within the partials view file?

Thanks

EDIT: Working on a tight deadline so with no solution in sight, I had to abandon Phil Sturgeon's template library and pick up Jens Segers' instead. http://www.jenssegers.be/blog/25/Codeigniter-template-library It is a little similar to Phil's but for some reason the partials are receiving and rendering my data properly. Also, Sergers' template library is very well documented. All is fine now, so I'll accept my own answer to problem (a). Thanks everyone.

  • 写回答

1条回答 默认 最新

  • douyingp82418 2012-09-05 00:48
    关注

    (a) So after a lot of fiddling around I have solved the first problem i.e. the redirects.

    In the config file, we have

    | If this is not set then CodeIgniter will guess the protocol, domain and
    | path to your installation.
    |
    */
    $config['base_url']=
    

    Apparently, I didn't set a base URL because I'm still on a development local server, I was going to set it to the www address when I upload to the test server. Codeigniter had been guessing all along, but the guess didn't work for my authentication redirects. I set the base url and all redirects were fine.

    (b) Now, to see why the partial views are not receiving data. Any help please? :(

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮