Class not found error when Controller has uppercase letters in class & its file name
I have setup Php 5.5, Phalcon 1.3.1 on 64bit Linux on a shared hosting server. I was able to run my application on Wamp on Windows 7 with Phalcon 1.3.1 & Php 5.5 but when I moved the application to production on a Linux 64 bit (most probably CentOS) I got the below error:
Fatal error: Uncaught exception 'Phalcon\Mvc\Dispatcher\Exception' with message 'CIAB\Controllers\ChooseprojectController handler class cannot be loaded' in /home/myproj/public_html/ciab/public/index.php:35
Stack trace: #0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('CIAB\\Controller...', 2)
#1 [internal function]: Phalcon\Dispatcher->dispatch()
#2 /home/myproj/public_html/ciab/public/index.php(35): Phalcon\Mvc\Application->handle()
#3 {main} thrown in /home/myproj/public_html/ciab/public/index.php on line 35
It looks like Phalcon doesn't allow uppercases in Controller class and file names except as the first letter. To get rid of this error I had to change the controller class name and the name of the file containing the class, i.e. ChooseProjectController.php, with a lower case 'P'. As mentioned earlier, it worked fine on Windows but on linux it didn't till I changed as above. You should easily be able to recreate this bug and it seems to be related to an older bug Error parsing annotations router #1040 bug. I am new to Github so I hope this is the correct way to inform you of a potential bug.
Regards
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Linux is case sensitive and Phalcon get the controller name, the first letter will be automatically converted to uppercase, other converted to lowercase.
I hope you consider this to be an issue and resolve it or is it that we can never define a controller class with multiple upper case letters in Phalcon.
I hope the devs consider this as not a bug. Phalcon handles this without any issue. But you must declare your classes in proper way - file name must be the same as the class name (case sensitive). There is a common pattern between various projects that uses this approach - class name corresponds to file name. And this shouldn't be altered.
It's annoying that Windows is case insensitive but it's not a bug in Phalcon anyway. Please follow good standards ;-)
I agree with you. El 30/06/2014 3:27, Maciej escribió:
I hope the devs consider this as not a bug. Phalcon handles this without any issue. But you must declare your classes in proper way - file name must be the same as the class name (case sensitive). There is a common pattern between various projects that uses this approach - class name corresponds to file name. And this shouldn't be altered.
It's annoying that Windows is case insensitive but it's not a bug in Phalcon anyway. Please follow good standards ;-)
— Reply to this email directly or view it on GitHub https://github.com/phalcon/cphalcon/issues/2572#issuecomment-47501901.
Todo tiene su tiempo y todo lo que se quiere debajo del cielo tiene su hora
VII Escuela Internacional de Verano en la UCI del 30 de junio al 11 de julio de 2014. Ver www.uci.cu
Jodator, the issue is not how Linux / Windows treat the file names - you are right on that point. My class name and file name cases were the same. It should have worked on both platforms. I think Phalcon has an issue that it is doing some case conversion as dreamsxin mentioned above. It should not. Doesn't Windows API return the file name to Phalcon as the actual case is? If it does, then there should be no issue. Let me know, if it is not a good convention to use camel case in file names or variable names in Php. I have shifted to Php from another programming environment recently.
@amsharma9 OK, maybe I misunderstood something reading your post, but in error you have that ChooseprojectController cannot be found so if you have ChooseprojectController.php and get this error then it is something wrong, but if you have ChooseProjectController.php then it's wrong class name or wrong file name.
As for naming I'm using camel case almost everywhere (with exception to view file names in volt) and I find it OK.
Thanks for clarifying. I will be specific - when I have ChooseprojectController in class and file name it works fine but when I have ChooseProjectController in both class and file name it doesn't work, because Phalcon is looking for a class ChooseprojectController in the latter case too - if I understand correctly, its an issue with Phalcon because it converts all controller class names to first letter uppercase and rest lowercase. Is that functionality of Phalcon acceptable. If yes, that implies I can never have a controller class like MyClassWithCamelCaseController because it will look for class MyclasswithcamelcaseController. Correct me if I am wrong.
You can do it. Your URL must look like choose_project/yourAction
@Baloche is right - I've overlooked that since I'm using custom routes for controllers and hadn't such issue before (only with action names).
@Baloche Sorry I didn't understand. Are you saying I should use underscores instead of camel case in controller names, i.e. each upper case is preceded by an underscore.
@amsharma9 Nope.
By default, Phalcon is translating URL adress www.example.com/choose_project/yourAction to a controller named ChooseProjectController and its method yourActionAction (if I'm recalling action name mapping correctly). This controller supposed to be in ChooseProjectController.php. So if you access www.example.com/chooseproject/yourAction' Phalcon will search forChooseprojectControllerinChooseprojectController.php`.
If you would like to have different URLs you can use custom routes for that.
That's right @jodator . @amsharma9 , take a look to phalcon vokuro example (https://github.com/phalcon/vokuro). In the app/controllers folder, you have a controller named UserControlController.php. This controller is called from a custom route (see it in app/config/routes.php)
The same thing happened to me. But not with every every name controller.
For example: both controller and file name TvGuideFeed (camelcase). And the response was something like: "could't find Tvguidefeed" (not camelcase).
The same thing happened to a Tasks Name of a command line application.
As soon as I changed the name TvGuideFeed for Feed the problem was solved.
The funny thing is that I get no error in the development environment (ubuntu -vagrant) I get the error in the integration environment (ubuntu - same configuration as development).
Guys, how is this not a bug? If the first character is uppercase, the rest can't just be bulldozed into lower case. Either you use camel case or you use some other standard. Forcing single-word controller names is just weird and counter intuitive. Naming convention is a big deal. Also, adding routes manually sounds very much like a workaround to me, rather than a proper solution. If there's implicit routing, it should use the same naming conventions as the rest of the framework.
Personally, I have a UrlCamelizer plugin that takes hyphened URLs (/choose-project) in accordance with Google's recommendations and converts to camel case for matching with the Controller / Action names. When the Controller name has been set, Phalcon rewrites my rewrites, breaking the URLs. This must be fixed, in my opinion.
Lastly, not to sound negative, I want to say that Phalcon is an AWESOME framework and I love it! Thanks for your hard work, Phalcon Team!
@dimhoLt I totally agree with you. According to me it is a bug. Why should phalcon force controller names. Maybe it aids them in code generation using developer tool so they kept this restriction but it will break anybody's site and it is not intuitive. It will leave developers wondering why their application doesn't work when everything is ok.
I have exact the same issue when I implement a cli application. I need to run the app like "php cli.php AnalyticsGetSearchRequest". I got error message like: "AnalyticsgetsearchrequestTask handler class cannot be loaded." As your can notice it change the upper case letter to lower case. To make it work I have to change my php file name from AnalyticsGetSearchRequestTask.php to AnalyticsgetsearchrequestTask.php. The class name defined in the file also need to be changed. To me this is a bug. Also I want to mention that the issue is on CentOS but not the MacOS.
@olivezuo The reason why it doesn't occur on MacOS is because it's case insensitive by default, which CentOS is not. I'd like to see this issue reopened from Phalcon, it is definitely a bug.
+1 vote for "this is a bug"
After reading through the entire thread once more, I need to draw more attention to another thing which I just mentioned - if you have a case insensitive operating system, such as MacOS or Windows, you can name your controller for example ChooseProjectController with corresponding filename, and the implicit route /ChooseProject/ActionName will work. This is, of course, because those operating systems don't make any difference between the names ChooseProjectController.php and ChooseprojectController.php - the file will be returned when PHP requests it.
However, when you use a case sensitive OS, such as Ubuntu, CentOS or any Unix-like (except MacOS, as previously mentioned) all routes with more than two uppercase letters (i.e. the very first, and the C in "Controller") will simply crash with an error because ChooseProject != Chooseproject.
Considering that case sensitive operating systems are used on 62 - 82 % of the web servers, this is simply not viable. Phalcon introduces inconsistencies with:
- Naming convention standards (PSR-0, CamelCase, namespacing etc ...).
- Their own chosen naming convention standards (PSR-0).
- Expected framework behavior.
Phalcon should only care about about the "Controller"-part, and it should be the developers' responsibility to make sure the routes match the file name from there on. Correctly named routes / controllers that get killed by the framework must be considered a bug.
@dimhoLt This nothing has to do with PSR-0 or namespacing, the framework always had worked like this and the standard is clear.
Controller name passed by the router is camelized by dispatcher so:
- controller-project becomes ControllerProject
- controllerproject becomes Controllerproject
- Controller-Project becomes ControllerProject
Action names are always passed lowercased to the view.
The framework currently provides ways to change these behaviors if you don't like them but we can't change the default behavior and break many applications just because you want to consider it a bug.