Rommel Paras
Rommel Paras

Reputation: 325

Only default controller is working, other controllers/routes are not

Just recently upgraded from Code Igniter 2.2 to Code Igniter 3.

Website is plaster.tv. The homepage works however any other URL is routed to this default_controller even though other routes are specified:

/application/config/routes.php

$route['default_controller'] = "home";
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['login'] = 'login';
$route['signup'] = 'signup';
$route['main'] = 'main';
$route['sendResetEmail'] = 'sendResetEmail';
$route['resetPassword'] = 'resetPassword';

Internal pages which is handled by other controllers, e.g. plaster.tv/signup doesn't work, but plaster.tv/?c=signup works.

/application/config/config.php:

$config['base_url'] = 'http://www.plaster.tv/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI'; //worked using AUTO in CI 2.2
$config['url_suffix'] = '';
$config['subclass_prefix'] = 'MY_';
$config['composer_autoload'] = FALSE;
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = TRUE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd';

/application/controllers/Home.php:

<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Home extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }

    public function index()
    {
    //... this part works
    }
}

/application/controllers/Signup.php:

<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Signup extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
  {
//... this class is never called when the URL is http://www.plaster.tv/signup but is called when the URL is http://www.plaster.tv/?c=signup 
 }
}

Hope someone could help me figure this out.

Upvotes: 1

Views: 4245

Answers (3)

Andrew Simeou
Andrew Simeou

Reputation: 91

I had the same issue and tried pretty much everything I'd read online bearing in mind I've developed many sites with Codeigniter before. But the one thing that I did change this time round was my machine and XAMPP set up. I found the issue was due to the configuration in XAMPP itself. The virtual host file wasn't configured correctly and although the default controller was working, anything else wouldn't.

What I did to rectify this was to ensure the follow lines were added to the httpd-vhosts.conf file in XAMPP:-

<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName localhost
    <Directory "C:/xampp/htdocs">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

And then each of my virtual hosts is set with the following tags:-

<VirtualHost *:80>
DocumentRoot "D:/Development Websites/testsite1/httpdocs"
ServerName testsite1.com
ErrorLog "logs/testsite1-error-log.log"
    <Directory "D:/Development Websites/testsite1/httpdocs">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
</Directory>

After doing this, all my controllers now work fine. It was nothing to do with CI config or .htaccess in the end.

Upvotes: 2

kennykee
kennykee

Reputation: 116

Same situation here after changing CI2 to CI3. All controllers including non-existent controller routed to the default controller.

To solve this, change $config['enable_query_strings'] = TRUE; to FALSE.

Upvotes: 3

DFriend
DFriend

Reputation: 8964

When you set $config['enable_query_strings'] = FALSE you force CodeIgniter (CI) into using query strings. That is why plaster.tv/?c=signup works but plaster.tv/signup does not.

To browse to a controller it is one way or the other, you cannot use both. If you do not want to use query strings to call a controller then you need

$config['enable_query_strings'] = TRUE;

This setting does not mean that you cannot append a query string to a URL used with CI. Any such query items will be accessible in controllers by using $this->input->get('some_key'); so long as $config['allow_get_array'] = TRUE;

Not related to your issue but you might like to know: The following lines are not needed and should be deleted

$route['login'] = 'login';
$route['signup'] = 'signup';
$route['main'] = 'main';
$route['sendResetEmail'] = 'sendResetEmail';
$route['resetPassword'] = 'resetPassword';

You only need to define routes if you want to remap CodeIgniter's normal URI relationship between a URL string and its corresponding controller/method.

Any of the above will can be browsed to with http://plaster.tv/name_of_controller without the need of any settings in routes.php - assuming there is an index() method in the requested controller.

Upvotes: 2

Related Questions