CodeIgniter Forums

Full Version: Problem with constructor and URI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]v1ctoria[/eluser]
Hi lads!

I'm actually building a simple website with members and I wanted to display member's page by just calling 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 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 :
Quote:404 Page Not Found
The page you requested was not found.

This is my class Member :
Code:
<?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:
Code:
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

El Forum

[eluser]danmontgomery[/eluser]
http://ellislab.com/codeigniter/user-gui...uting.html

Code:
$route['member/(:any)'] = 'member/index/$1';

El Forum

[eluser]v1ctoria[/eluser]
Awesome, Thanks a lot Noctrum!

I totally missed that part, clearly not focusing on that but more on why it's not working in __consruct.

Just to try to understand, is it possible to do what I was trying to do in the constructor? if yes, what was wrong?

El Forum

[eluser]danmontgomery[/eluser]
Never tried it... It's probably possible, but there are other, better ways of doing it. _remap, for example.

El Forum

[eluser]Unknown[/eluser]
Hi v1ctoria,
Have you solve the problem ? if yes, please let me know solution. I am facing same problem Sad .

El Forum

[eluser]InsiteFX[/eluser]
Use _remap method
Code:
// ./application/config/routes.php
// ------------------------------------------------------------------------
/**
* DO NOT! REMOVE THIS LINE
* This is for the members _remap. Must be the last line in routes file!!!
* ------------------------------------------------------------------------
*/
$route['(.*)'] = 'welcome/index/$1';
// ------------------- -== DO NOT REMOVE! the above line ==- --------------

// Controller index method
// --------------------------------------------------------------------

/**
* index()
*
* index - default method called.
*
* @access public
* @return void
*/
public function index($member = NULL)
{
    if ( ! empty($member))
    {
        // URL contains a member name!
    }
    else
    {
        // No member name passed!
    }
}

// _remap method

// --------------------------------------------------------------------

/**
* _remap()
*
* Remaps the URL segments.
*
* @access private
* @param string
* @param array
* @return mixed
*/
public function _remap($method, $params = array())
{
    if (method_exists($this, $method))
    {
        return call_user_func_array(array($this, $method), $params);
    }

    show_404();
}