1
votes

I am trying to develop a social network where people can visit mysite.com/username. I used the _remap function and I got it working, however it is not loading any of my other controllers. Can someone be of assistance please?

This is my default controller:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Welcome extends CI_Controller {

    public function index($username = NULL)
    {
        $this->load->model('user_model');
        if ($this->user_model->is_a_username($username)) {
            $data['title'] = $username;
            $data['main_content'] = 'users/profile_page';
            $this->load->view('shared/template',$data);
        } else {
            $this->home();
        }
    }

    public function _remap($method, $params = array())
    {
        if (method_exists($this, $method))
        {
            return call_user_func_array(array($this, $method), $params);
        }
        show_404();
    }

    public function home()
    {
        if ($this->ion_auth->logged_in()) {
            $data['title'] = 'Carnect';
            $data['main_content'] = 'users/wall_page';
            $this->load->view('shared/template',$data); #if logged in show the user's wall
        } else {
            $data['title'] = 'Carnect';
            $data['main_content'] = 'welcome/index';
            $this->load->view('shared/template',$data); #if not logged in show the home page
        }       
    }
}

and this is my routes file:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$route['default_controller'] = "welcome";
$route['404_override'] = '';


$route['login'] = "auth/login";
$route['logout'] = "auth/logout";
$route['register'] = "auth/create_user";


/*$route['news'] = "news/index";
$route['politics'] = "politics/index";
$route['culture'] = "culture/index";
$route['messages'] = "messages/index";*/


$route['(:any)/(:any)'] = "$1/$2";
$route['(:any)/(:any)/(:any)'] = "$1/$2/$3";
$route['(.*)'] = 'welcome/index/$1';

An example of one of the controllers that will not load..

session_start();

class News extends CI_Controller {

    function News()
    {
        parent::Controller();
    }

    function index() {
        $data['title'] = 'Politics';
        $data['main_content'] = 'news/index';
        $this->load->view('shared/template',$data);
    }
}

2
Is codeigniter still alive? Let it die already! - ChocoDeveloper

2 Answers

1
votes

I am working on a project that has a similar requirement on those urls.

I did it by adding route like this:

$routes['news'] = 'news/index';

or

$routes['news'] = 'news';

which are exactly lines that you've commented.

Sadly, It isn't possible without those lines (at least I couldn't do it).

If your URL was: example.com/news/index, it would match the rule $routes['(:any)/(:any)'], but if it was example.com/news, it wouldn't match anything and go to your last rule.

CodeIgniter's Routing doesn't take real segments, but the uri segments shown in the url. Therefore, your url example.com/news would be interpreted as a $username = news.

You'd have to do this uri routing for each url which only has 1 uri segment. You'd want to make sure no user has the same username with your controllers or he/she can never visit the user page.

0
votes

You have to write a rule in application/config/routes.php file just add a following line

$route['(:any)'] = "welcome/index/$1";

You can access by using http://example.com/username if your have added .htaccess rule for removing index.php file from url or you can access like http://example.com/index.php/username