Namespacing a component (2.5 and 3.x)

311 views
Skip to first unread message

Brad Gies

unread,
Aug 18, 2013, 11:28:44 PM8/18/13
to joomla-de...@googlegroups.com

Just a quick question. Can anyone point me to a good example or
documentation for namespacing a component (the PHP code) for Joomla!? I
was going to use the same namespacing conventions I use for other PHP
work, but then realized there might be some real benefits to doing it
the Joomla! way. I looked through some platform code, and found this by
Don Gilbert: https://github.com/joomla/joomla-platform/issues/1787 and
this: https://gist.github.com/AmyStephen/4520559, but I didn't find any
document that explicitly mentioned how a third part component should be
namespaced to work best with Joomla!.

So... My component is an Admin only component, no front end at all.
Should my namespace be :

<php
namespace com_mycomponentname;
// No direct access
defined('_JEXEC') or die;

class MyClass {

Or should it be something like :
<php
namespace Administrator\com_mycomponentname;
OR
namespace Joomla\Administrator\com_mycomponentname;



Sincerely,

Brad Gies
----------------------------------------------
bgies.com maxhomevalue.com
idailythought.com greenfarminvest.com
----------------------------------------------

Dmitry Rekun

unread,
Aug 19, 2013, 5:28:43 AM8/19/13
to joomla-de...@googlegroups.com
Hi Brad!

Currently there are no rules for namespacing simply because CMS is not using namespaces. Why do you want to namespace your component? There are no benefits doing it, because all classes in CMS has unique names that follows CMS convention. This convention helps CMS to autoload these classes.

Dmitry

Donald Gilbert

unread,
Aug 19, 2013, 11:37:04 AM8/19/13
to joomla-de...@googlegroups.com
Currently, the CMS does not support namespaced components out of the box. You'd have to write a compatibility layer between the component code and Joomla. It's possible, and would go a long way towards figuring out how to use namespaced code in the CMS (meaning, maybe you could contribute your findings back one day) and it would definitely be worth while.

But that said, no, there are no examples, because as far as I know, no one has done fully namespaced components.

I have, however, started a blog series on how to structure your component to use the Joomla Prefix-based Autoloader to run your component, rather than the old style. It's a step in the direction of getting namespace support for Joomla components, so it would be worth reading: http://dongilbert.net/structuring-your-joomla-component-for-autoloading/


--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To post to this group, send an email to joomla-de...@googlegroups.com.
Visit this group at http://groups.google.com/group/joomla-dev-general.
For more options, visit https://groups.google.com/groups/opt_out.

Nikolai Plath

unread,
Aug 19, 2013, 12:57:06 PM8/19/13
to joomla-de...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Not sure on what to spend my time until your next blog post on this
topic....

Thanks Don for sharing this.
>>> https://github.com/joomla/**joomla-platform/issues/1787<https://github.com/joomla/joomla-platform/issues/1787>and
>>>
>>>
this:
https://gist.github.com/**AmyStephen/4520559<https://gist.github.com/AmyStephen/4520559>,
>>> but I didn't find any document that explicitly mentioned how a
>>> third part component should be namespaced to work best with
>>> Joomla!.
>>>
>>> So... My component is an Admin only component, no front end at
>>> all. Should my namespace be :
>>>
>>> <php namespace com_mycomponentname; // No direct access
>>> defined('_JEXEC') or die;
>>>
>>> class MyClass {
>>>
>>> Or should it be something like : <php namespace
>>> Administrator\com_**mycomponentname; OR namespace
>>> Joomla\Administrator\com_**mycomponentname;
>>>
>>>
>>>
>>> Sincerely,
>>>
>>> Brad Gies ------------------------------**----------------
>>> bgies.com maxhomevalue.com idailythought.com
>>> greenfarminvest.com
>>> ------------------------------**----------------
>>>
>>> --
>> You received this message because you are subscribed to the
>> Google Groups "Joomla! General Development" group. To unsubscribe
>> from this group and stop receiving emails from it, send an email
>> to joomla-dev-gene...@googlegroups.com. To post to
>> this group, send an email to
>> joomla-de...@googlegroups.com. Visit this group at
>> http://groups.google.com/group/joomla-dev-general. For more
>> options, visit https://groups.google.com/groups/opt_out.
>>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSEk5iAAoJEEOVpESW4ZV7OU0H/09ZVrtTl9nmfqNKs/hLjBQX
5RcZ3qo0EPezIAKW3DUvF193vv3i//Fh85R2Dq4Cj/EU/WAArkb51ie5GTfXXBmm
nmEaCw4nqzfzS5d7P+Muh4YaK0zCIyh65MVmPkvG6kWvA7m6amQjcbdF0S4kwzct
AfWr3BAwM7+wC2Zq7aXzf/bCMcHYpUB4q3Ys0h9H6Q+9oaUS6cDP18u4P4ye0HGd
4pxoNZnXCBP/81K7nR5ZCyAhlpkzdPzXnuuL5xq1i4btW+2QghZ9IUbrrDO14Ha5
BByNXlrHWMoQ4cXvgOx53JFUR2g2NjnhRMXfratIPq3AOWPoWtGjfm3lAMSxc0w=
=VXkI
-----END PGP SIGNATURE-----

Herman Peeren

unread,
Aug 19, 2013, 2:23:46 PM8/19/13
to joomla-de...@googlegroups.com
On Monday, 19 August 2013 17:37:04 UTC+2, Donald Gilbert wrote:
(... ) there are no examples, because as far as I know, no one has done fully namespaced components.
 
I know one: me. Working on a custom one now. Will publish an example soon. First the component I'm workin on has to be finished to be used by my (eager waiting) customer.

I've been using namespaces in Joomla CMS ever since I 'm working with Doctrine ORM in Joomla. I've also used the Doctrine autoloader ever since (putting Namespace - Directory pairs in the SPL-autoload queue).

I'm doing things a bit different; looking for better ways than the Joomla way. If you don't like that: don't read further. But I hope that in the end this will give us some nice ideas to use in a future Joomla-version. As you might know I've always disliked the separation in front-end and back-end: because they use the same domain model. So, I have since some years done all modelling in the back-end. Recently I had put all components (including modules and plugins) in one extensions-folder; no separation between front-end and back-end. But to get the Joomla menu-item-types automatically working I had to define view-folders, which caused double code... I switched back to putting everything in the back-end, except for the front-end views and the front-end entry-file (which triggers a onComponentStart to dispatch my component). In the back-end I have one controller (named component.php) and one model (model.php), a folder with entities (bit comparable with the tables-folder, but with richer behaviour of the entities) and a views-folder. I now have 2 namespaces for every component: a front-end and a back-end namespace. Almost everything is in the backend namespace, only the front-end views are in the front-end namespace.

In my experience, it is no problem to use namespaces in Joomla CMS (I mainly write for 2.5 now, but it is compatible with 3.x). The only thing is to not forget the backslash for the main namespace the "normal" Joomla code (which I gradually use less) is in.

I'll publish some code soon on jooctrine.org ; let you know. I hope it will inspire some people. Sorry, it takes me so long before publishing anything; under serious time pressure while not being one of the fastest.

- Herman

Donald Gilbert

unread,
Aug 19, 2013, 2:59:20 PM8/19/13
to joomla-de...@googlegroups.com
That's awesome Herman! Looking forward to you publishing that as an example.

The "problem" with using namespaces is that if you extend JControllerLegacy, and then use JControllerLegacy::getInstance('MyComponent'), it will fail if you don't have a class named "MyComponentController" - it's not compatible to use with namespaces. That's where I said you have to have a layer between your component code and the CMS in order to facilitate this.


--

Herman Peeren

unread,
Aug 19, 2013, 4:07:20 PM8/19/13
to joomla-de...@googlegroups.com
Because I don't use JControllerLegacy, I don't have that problem. I have used JViewLegacy until recently (or better said: \JViewLegacy) and it needs a getName() in the model you use (which I extended from jooctrine\Model), but didn't give much problems further. I am working on a jooctrine\View (putting some FoF and JLayout ideas in that).

I tried to change as little as possible to get it all working without problems in Joomla, but changed my view.html.php files to HtmlView.php, which gives me the class mycomponentnamespace\views\viewname\HtmlView (a dot is not allowed in a classname). I start up Doctrine with the Composer autoloader and further use the Doctrine autoloader. My composer.json is under /libraries and I have deployed the whole vendor-folder (with Doctrine etc.) online without change (still investigating security issues with that).

I had my Joomla folder-structure unchanged and therefore used namespaces all small-caps (no first letter capital). But all files with a class or interface in a namespace are starting with a capital letter (which is not necessary for classnames are not case sensitive). That way I avoided problems using the current directory structure in the Joomla CMS (didn't try it out, but otherwise probably had to make some adjustments to the autoloader).

Exciting times.

Donald Gilbert

unread,
Aug 19, 2013, 4:20:07 PM8/19/13
to joomla-de...@googlegroups.com
"I don't use JControllerLegacy" - which just reinforces my point, that Joomla does not support it out of the box if you follow the guidelines on how you're "supposed" to create your component. (And I'm with you - I don't like using those either :) ) Of course, you can do whatever you want once you get into the component entry file. David Hurley and I are even working on a POC for having code that will both install as a joomla component as well as a completely standalone application.

Exiting times indeed. :)

Andrew Eddie

unread,
Aug 19, 2013, 6:34:43 PM8/19/13
to joomla-de...@googlegroups.com
On Monday, 19 August 2013 13:28:44 UTC+10, Brad wrote:
Or should it be something like :
<php
namespace Administrator\com_mycomponentname;
OR
namespace Joomla\Administrator\com_mycomponentname;

Just in terms of namespacing, I'd go with a <Vendor>/<Package> strategy where <Vendor> maps to "your brand" and "Package" would possible map to the extension type. I'd even consider storing the namespaced code in the /libraries folder which allows you to keep the Joomla extensions extremely light.

My 2c.

Regards,
Andrew Eddie

Herman Peeren

unread,
Aug 20, 2013, 7:42:39 AM8/20/13
to joomla-de...@googlegroups.com
Putting namespaced code in the /libraries folder is indeed a good spot, but I limit that mainly to code I use in multiple extensions.

I started to use namespaces in components because I didn't use Joomla's controller and model, and wanted to easily autoload a model and view-classes. I can now autoload any class from any namespaced component without even having to (re)write an autoloader for that. There probably are other ways to do that too, but this is the solution I choose... and it worked.

- Herman
Reply all
Reply to author
Forward
0 new messages