Skip to content

Tutorial controller Pages and file_exists() case sensitivity #1030

Description

@dafriend

The PHP function file_exists() follows OS handling of case sensitivity. In the tutorial the view file is to be named using uppercase first - Home.php . Then the tutorial code uses a conditional in Pages::view.

public function view($page = 'home')
{
	if ( ! file_exists(APPPATH.'Views/Pages/'.$page.'.php'))
	{
		// Whoops, we don't have a page for that!
		throw new \CodeIgniter\PageNotFoundException($page);
	}

When the url http://example.com//index.php/pages/view is requested, and the OS is case-sensitive, the above conditional results in the exception being thrown. "home.php" is not a file that exists in a case-sensitve OS.

In the context of the tutorial, there are many ways to fix this issue. Any one of these will work

  • Change the file name to "home.php" (lower case first)
  • Redefine the method to public function view($page = 'Home');
  • Make sure $page will match the designed file name, i.e. $page = ucfirst($page);
  • Require that view files use the UC first naming convention, which doesn't really eliminate the need for $page = ucfirst($page);

Seems to me this problem begs the question about a file naming convention for view files. There is nothing in the CI4 PHP Style Coding Guide regarding naming of "view" files. Should there be? Or does the framework need a helper that looks twice (UC-first and LC-first) for view file names? Or does the documentation need to point out case-sensitivity as a potential issue for developers? All of the above?

I'm happy to contribute a PR to the tutorial code and/or documentation if given some direction on what needs to be done.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions