Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin: custom namespace? #7569

Closed
mehov opened this issue Jan 7, 2023 · 6 comments
Closed

Plugin: custom namespace? #7569

mehov opened this issue Jan 7, 2023 · 6 comments

Comments

@mehov
Copy link
Contributor

mehov commented Jan 7, 2023

https://book.cakephp.org/4/en/plugins.html

Plugin should define their own top-level namespace. For example: DebugKit. By convention, plugins use their package name as their namespace. If you’d like to use a different namespace, you can configure the plugin namespace, when plugins are loaded.

How?

@mehov
Copy link
Contributor Author

mehov commented Jan 7, 2023

Tried putting the namespace I want into composer.json, and using it across the plugin files (#7568) - doesn't work with routing, as the system is still looking for the namespace derived from plugin name/path.

@LordSimal
Copy link
Contributor

LordSimal commented Jan 7, 2023

"Private" plugins live in the plugins directory and are NOT autoloaded by composer
"Usual" plugins installed via e.g. composer require cakephp/debug_kit live in the vendor directory ARE autloaded.

You have to specify in your root composer.json (NOT the plugins composer.json) which namespace maps to which folder relative to that composer.json
see https://book.cakephp.org/4/en/plugins.html#manually-autoloading-plugin-classes

Just like you did in #7568 you have to define

"CakePress\\": "plugins/cakephp-wordpress/src"

in your root composer.json and regenerate the autoloader via composer dump-autoload to let composer know where your namespace lives. With that at least PHP knows which file it has to load if you reference a class in the namespace CakePress

The other problem you have in #7568 is probably related to the fact, that you don't have a PostsController inside your plugins/cakephp-wordpress/src/Controller directory. At least you didn't mention it.

@mehov
Copy link
Contributor Author

mehov commented Jan 7, 2023

Hi @LordSimal, thank you for replying

You have to specify in your root composer.json

Yes, I did put the namespace I wanted into the root (not plugin) composer.json file and I did run composer dump-autoload afterwards, as well as bin/cake cache clear_all just to be sure.

At least you didn't mention it.

You're right, I just never mentioned it - sorry about that. I do have it and once I used the namespace CakePHP wanted (the camelcase version of the plugin folder name) everything routed correctly and I got my test response from that Posts controller.

@LordSimal
Copy link
Contributor

If you feel like the docs can be improved you can of course provide a PR which clarifies what confused you in the first place.
Regarding any future support questions I can recommend you join our slack/discord server or our discourse forum.
See https://cakephp.org/get-involved#getHelp

@mehov
Copy link
Contributor Author

mehov commented Jan 7, 2023

Sending a PR implies knowing the solution, but I don't have any. I have tried several things, but I couldn't get my plugin to use the custom namespace I wanted and have functioning routes. I am already on the Slack channel, but that is indeed for one-off support questions, while here on Github I am asking someone who has the answer to improve the docs.

@dereuromark
Copy link
Member

If you need examples, you can always check awesome list of plugins:
https://github.com/FriendsOfCake/awesome-cakephp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants