1

I am setting up a CodeIgniter project and so far everything has been going well. All my routes and controllers are working correctly except for one. While I am new to CodeIgniter I have been programming in PHP for close to five years so I have a bit of experience. If anyone could take a look at the code samples below and let me know what I am doing wrong, that would be great.

Apparently the application does recognize the route, but it keeps complaining about its inability to find the URI.

Controller:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Profile extends CI_Controller {
    function __construct() {
        parent::__construct();

        $this->load->helper('url');
    }

    public function Profile($page = 'profile-page')
    {
        if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
        {
            // Load error page
            show_404();
        }

        // Capitalize the first letter
        $data['title'] = ucfirst($page);
        $data['pageType'] = "profile-page";

        // Load pages
        $this->load->view('templates/header', $data);
        $this->load->view('templates/topbar', $data);
        $this->load->view('profile');
        $this->load->view('templates/footer', $data);
        //
    }
}

Next is my Routes:

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

// All custom routes go below this line.

$route['contact'] = 'pages/contact';
$route['signup'] = 'auth/signup';
$route['login'] = 'auth/login';
$route['profile'] = 'profile/profile';

I have verified that all the view files are in their correct folders and there are no typos. However the Profile route keeps failing. All the other routes work correctly, except for the Profile route.

I have tried this on Windows 10 with XAMMP, Mac OS X Sierra with MAMP and CentOS 7 with Apache/PHP/MySQL.

Thanks.

cp-stack
  • 785
  • 3
  • 17
  • 40

4 Answers4

1

The problem is your code enters into this condition & it is not able to find the profile-page.php file at that path and showing error.As profile function is calling properly without any issue as i checked

if ( ! file_exists(APPPATH.'/views/'.$page.'.php'))
        {
            echo "test";die;
            // Load error page
            show_404();
        }
aishwarya
  • 272
  • 3
  • 10
  • While not the answer to my problems, your question did point me in the right direction. I got it working and it turned out to be a missing file. – cp-stack Mar 12 '17 at 13:29
0

It's been a little while since I have written CodeIgniter routing, but I seem to remember the method names being case sensitive, so the Profile having a capital 'P' could be causing an issue?

The way I'd usually go about debugging this though is to try renaming the method, renaming the controller; the idea being you can figure out if the issue is in the routing, or if there's some unseen typo or other issue within the code, or whether its the route itself

Not sure if this helps? Good luck getting it resolved though!

0

Mac OSX is case sensitive, make sure the filenames in controllers match the case you are trying to access in your routes. Also it would help if you post the output too. I had a similar problem a while back which I solved by renaming my filenames in accordance to routes defined. Please refer to my answer HMVC codeigniter works on local server but not on web server here.

Community
  • 1
  • 1
Zeeshan Cornelius
  • 308
  • 1
  • 4
  • 15
  • I had the same issue on Windows 10 and Microsoft is not case sensitive. But I will give your solution a try and let you know. – cp-stack Mar 09 '17 at 18:14
0

In the default controller you can not use sub folders if you are using CI 3 unless you have modified MY_Router.php

in application/core/MY_Router.php

This is the original Question here

<?php

class MY_Router extends CI_Router {

    protected function _set_default_controller() {

        if (empty($this->default_controller)) {

            show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
        }

        // Is the method being specified?

        if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
            $method = 'index';
        }

        // This is what I added, checks if the class is a directory

        if( is_dir(APPPATH.'controllers/'.$class) ) {

            // Set the class as the directory

            $this->set_directory($class);

            // $method is the class

            $class = $method;

            // Re check for slash if method has been set

            if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
                $method = 'index';
            }
        }


        if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {

            // This will trigger 404 later

            return;
        }

        $this->set_class($class);
        $this->set_method($method);

        // Assign routed segments, index starting from 1

        $this->uri->rsegments = array(
            1 => $class,
            2 => $method
        );

        log_message('debug', 'No URI present. Default controller set.');
    }
} 
  • Thanks for the answer. However, all my routes work except the profile route. That is the only route that is not working. The default route works as well as all the others listed. – cp-stack Mar 09 '17 at 20:34