Monday, January 27, 2014

Re: What's the correct method for using Traits and Namespaces for CakePHP 2?

For the moment, even though PHP 5.5 has namespaces, it just seems to work if you don't use them (namespaces, that is).

You will need to have an App::uses() statement to use load the trait, and you'll also need the 'use' statement in the class.

And given that namespaces are not in use, also keep 'app' as 'app' and not 'App'.  Although replacing 'app' with APP_DIR would be better for CakePHP 2.4.5 projects that have selected a different name for their project, and don't have the HTTP root pointing to webroot (in the case of integration with legacy apps).

Regards
Reuben Helms

On Tuesday, 28 January 2014 10:52:07 UTC+10, Reuben wrote:
My original question on Stack Overflow [http://stackoverflow.com/questions/21394852/whats-the-correct-method-for-using-traits-and-namespaces-for-cakephp-2], and content copied here, should that disappear.

I'm using CakePHP 2.4.5 and PHP 5.5, and would like to use a trait.

I have a trait in Utility/VariablesTrait.php called VariablesTrait.

To take advantage of namespaces, I've given it a namespace of App\Utility\VariablesTrait, since Utility\VariablesTrait seems a bit too global, and the former would work better with CakePHP 3.

In my class that I want to use it in, I have the use App\Utility\VariablesTrait; statement in the class. For backup, I also have a App::uses('VariablesTrait', 'Utility'); statement at the top of the file. I'm not sure if the SPL autoloader is used when looking for traits, which is why I was going for namespaces in the first place.

The small issue is that the app directory is app, and since directory structures should match namespaces (I think), I renamed it to App. However, CakeRequest::_base() hardcodes app, so determining the controller doesn't work so well.

So, I'm trying to determine if that's a CakePHP bug, or if there is a more appropriate way of using traits in CakePHP 2.


Regards

Reuben Helms

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: