CI 构造函数和 URI 的问题

发布于 2024-11-04 00:22:14 字数 2385 浏览 1 评论 0 原文

我实际上正在与会员建立一个简单的网站,我想通过调用 http://www 来显示会员页面.mywebsite.com/member/membername

我的类成员实际上运行良好,但我已将代码放入索引函数中,使页面成员可以在 http://www.mywebsite.com/member/index/membername,这实际上并不是很有用。

所以经典的事情是将我的代码放入构造函数中,但我收到错误:

404 Page Not Found 找不到您请求的页面。

这是我的班级成员:

<?php

class Member extends CI_Controller {


    function __construct()
    {
           parent::__construct();


           $segment_id = $this->uri->segment(2, 0);
           echo 'segment found ='.$segment_id;
           if ($segment_id == FALSE)
            {
                   echo '- No Segment -> Printing all members ';
                   $data = array();
                   $this->layouts->set_title('Welcome!');

                   $this->layouts->add_aside('newsletter_view');

                   if($query = $this->membership_model->show_all())
                    {
                        $data['records'] = $query;
                    }

                    $this->layouts->view('show_member', $data);
            }
            else
            {
                    echo '- Segment found -> Printing the member ';
                    $data = array();
                    $this->layouts->set_title('Welcome!');

                    $this->layouts->add_aside('newsletter_view');

                    if($query = $this->membership_model->show_member())
                    {
                            $data['records'] = $query;
                    }

                    $this->layouts->view('show_member', $data);
            }

    }

} 

为了完成,这是我的模型功能:

function show_member()
    {
                echo 'model show_member working';
        $this->db->where('username', $this->uri->segment(2));
        $query = $this->db->get('members');

                return $query->result();
    }

        function show_all()
        {
                echo "model show_all working";
                $query = $this->db->get('members');
        return $query->result();
        } 

您会发现一些回显。所有这些都可以完美地与带有 /member 或 /member/segment 的 url 一起使用,正如我所悲伤的那样,如果我将其放入索引中,所有这些都可以正常工作,

非常感谢您的帮助

I’m actually building a simple website with members and I wanted to display member’s page by just calling http://www.mywebsite.com/member/membername

My class member is actually working well, but I had put the code in the index function making page member accessible at http://www.mywebsite.com/member/index/membername, which is actually not really useful.

So the classic things was to put my code in the constructor, but I get the error :

404 Page Not Found
The page you requested was not found.

This is my class Member :

<?php

class Member extends CI_Controller {


    function __construct()
    {
           parent::__construct();


           $segment_id = $this->uri->segment(2, 0);
           echo 'segment found ='.$segment_id;
           if ($segment_id == FALSE)
            {
                   echo '- No Segment -> Printing all members ';
                   $data = array();
                   $this->layouts->set_title('Welcome!');

                   $this->layouts->add_aside('newsletter_view');

                   if($query = $this->membership_model->show_all())
                    {
                        $data['records'] = $query;
                    }

                    $this->layouts->view('show_member', $data);
            }
            else
            {
                    echo '- Segment found -> Printing the member ';
                    $data = array();
                    $this->layouts->set_title('Welcome!');

                    $this->layouts->add_aside('newsletter_view');

                    if($query = $this->membership_model->show_member())
                    {
                            $data['records'] = $query;
                    }

                    $this->layouts->view('show_member', $data);
            }

    }

} 

And to complete, this is my model function:

function show_member()
    {
                echo 'model show_member working';
        $this->db->where('username', $this->uri->segment(2));
        $query = $this->db->get('members');

                return $query->result();
    }

        function show_all()
        {
                echo "model show_all working";
                $query = $this->db->get('members');
        return $query->result();
        } 

You will find some echo into. All are perfectly working going with a url with /member or /member/segment and as I sad, all is working if I’m putting it in Index

Thanks a lot for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫离伤花 2024-11-11 00:22:14

为什么不使用路由呢?

例如,添加路由 route['member/(:any)'] = "member/index/$1"; 应该可以解决问题(但尚未测试)。

Why not use routes for this?

For example, adding the route route['member/(:any)'] = "member/index/$1"; should do the trick (haven't tested it though).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文