Welcome Guest, Not a member yet? Register   Sign In
BaseController and controller documentation mismatch
#1

CodeIgniter's official distribution provides the BaseController class. The class name has two uppercase characters. Although
 in CodeIgniter's documentation is written (section controllers and Routing - Controllers):


Quote:!IMPORTANT

Controller class names MUST start with an uppercase letter and ONLY the first character can be uppercase.


Furthermore is mentioned that this example is not valid:


Quote:<?php namespace App\Controllers;

use CodeIgniter\Controller;

class HelloWorld extends Controller {

}
Actually BaseController has been exactly implemented as what is described to be invalid. Though it seems to be that BaseController works but not base_controller or anything similar. Huh


Who is right?
Reply
#2

Both are correct. You can extend BaseController (or any other Controller using the same naming scheme), as long as you don't access it with an url.

http://localhost/helloworld won't find HelloWorld file, as it dosen't know what letter you have capitalized. Therefor it should be Helloworld.

HOWEVER as you may already have tested, it's working right?! That means you are using Windows. Windows dosen't care about CaPiTaLiZaTiOn, but you generally have a Linux server for production. And now your application are broken. As you don't have a Capitalization.

If you are like me, only using HelloWorld for my naming scheme on your files, you can define your files in the /app/Config/Routes.php
$routes->get('helloworld', 'HelloWorld::index');

Now CodeIgniter will find it again on Linux.

TLDR: It's because CodeIgniters auto-magic resolver (setAutoRoute) from url to controller.
Reply
#3

Thanks for the answer (no, I am not using Windows but another non-case-sensitive file system for testing).

I never understood CodeIgniter's naming scheme for controllers and the reason for it. The relevant part of the URL scheme for identifying the controller is case sensitive, or?! Perhaps there is somewhere in CodeIgniter a function that creates from a generic name the name of a controller?
Reply
#4

There are a function just doing that, and that are being set with setAutoRoute.

Turning "helloworld" into "Helloworld".

newspage into Newspage

etc

It's a magic function so that you don't need to define all your routes.
Reply
#5

Yes, I know. But I do not see any benefit of doing so. Calling ucfirst on something is not better than just keeping it. The segments, e.g. names of the controller, are stored in the case-sensitive section of the URL. There is no reason (at least I do not know) to modify that section during internal processing. If the user (or programmer of the website) made a spelling mistake, it does not work. Changing this (from my current knowledge) to something randomly chosen does not make things better. If website designer forgot to name the controller or directory correctly it will not work either, now.

I just do not understand why it is necessary or where is the benefit of calling ucfirst on controller names and their directories.
Reply
#6

Generally speaking all website urls are lowercase. And the only way we can find controllers and methods (automatically) are by setting a naming convention. So the baseline are lowercase urls and not StudlyCaps urls.

Our controller standard are Helloworld, as it's the closes we can come with an url autoloader and PSR-1. And lowercase methods, as it's the closes too camelCase.

Our autoloader are based on namespacing specification in PSR-4 and all subfolders should be StudlyCaps.
PHP Code:
\<NamespaceName>(\<SubNamespaceNames>)*\<ClassName

That's why we are enforcing this, to keep it as close to the standard set by PHP-FIG. You can always disable the autoloader and define everything yourself in /app/Config/Routes.php or by extending our /system/Router.php and replacing it.
Reply
#7

Hi,

sorry, but it is not the intention to start here a big debate, I just want to clarify some things.

It is true that a lot of URL are lowercase but they do not have to be. If you like to follow PSR-1, then just name the URL http://myURL.com/MyController/MyFirstParameter.

The myURL.com part is case-insensitive. So, you may also write it myurl.com. But "...MyController/MyFirstParameter" is case-sensitive and not identical with "...mycontroller/myfirstparameter".

I do not see any need why CodeIgniter manipulates the URL.

If somebody likes to have the functionality of manipulating the URL, a flag might do the job. Otherwise, the URL should stay as it is. That's my opinion but I also see that this cannot be change anymore. Unless, you put a (new) flag in the configuration options that disables the URL manipulation. Then, everybody can name the directories and names as he/she likes.
Reply
#8

(This post was last modified: 04-19-2020, 11:48 AM by jreklund.)

Hi, no worries mate. It's in "CodeIgniter 4 Discussion" after all. :-)

There are nothing stopping you from using http://localhost/HelloWorld, it will find it just fine. But it can't find HelloWorld with http://localhost/helloworld, unless you define it in Config. It also can't find helloworld as we force it to be Helloworld.

You can however type HeLloWoRlD if you like, as long as the file name start with capitalized letter you will be fine.

ucfirst are there to enforce PSR-4 standard, but it dosen't have a built in dictionary, so it dosen't know when to capitalize the next word. That's why the documentation states that you MUST use Helloworld, as it can't find anything else automatically. (with an lowercase url)

You can use this url just fine:
http://localhost/HelloWorld/echoThisMessage

But you will never be able to load HelloWorld with helloworld, that's what the big Note is all about. (Unless manually setting it in Config)

"That's my opinion but I also see that this cannot be change anymore."
I don't know if CodeIgniter 2 did anything different, but the autoloader behaves the same in CodeIgniter 3.
Reply
#9

Someone should fact check me on this, but with CI4 you can name your controller using any character-case scheme you want IF you define a route for the controller. The UC first is mandatory only if you have CI figure out the controller from the URL alone.
Reply
#10

(04-21-2020, 07:23 PM)dave friend Wrote: Someone should fact check me on this, but with CI4 you can name your controller using any character-case scheme you want IF you define a route for the controller. The UC first is mandatory only if you have CI figure out the controller from the URL alone.

That is correct.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB