-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add documentation for loadPlugin() #5025
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -280,6 +280,56 @@ autoloader once you've created your plugin: | |
|
|
||
| $ php composer.phar dumpautoload | ||
|
|
||
| .. _plugin-routes: | ||
|
|
||
| Plugin Routes | ||
| ============= | ||
|
|
||
| Plugins can provide routes files containing their routes. Each plugin can | ||
| contain a **config/routes.php** file. This routes file can be loaded when the | ||
| plugin is added, or in the application's routes file. To create the | ||
| ContactManager plugin routes. Put the following into | ||
| **plugins/ContactManager/config/routes.php**:: | ||
|
|
||
| <?php | ||
| use Cake\Routing\Route\DashedRoute; | ||
| use Cake\Routing\Router; | ||
|
|
||
| Router::plugin( | ||
| 'ContactManager', | ||
| ['path' => '/contact-manager'], | ||
| function ($routes) { | ||
| $routes->get('/contacts', ['controller' => 'Contacts']); | ||
| $routes->get('/contacts/:id', ['controller' => 'Contacts', 'action' => 'view']); | ||
| $routes->put('/contacts/:id', ['controller' => 'Contacts', 'action' => 'update']); | ||
| } | ||
| ); | ||
|
|
||
| The above will connect default routes for your plugin. You can customize this | ||
| file with more specific routes later on. | ||
|
|
||
| Before you can access your controllers, you'll need to ensure the plugin is | ||
| loaded and the plugin routes are loaded. In your **config/bootstrap.php** add | ||
| the following:: | ||
|
|
||
| Plugin::load('ContactManager', ['routes' => true]); | ||
|
|
||
| You can also load plugin routes in your application's routes list. Doing this | ||
| provides you more control on how plugin routes are loaded and allows you to wrap | ||
| plugin routes in additional scopes or prefixes:: | ||
|
|
||
| Router::scope('/', function ($routes) { | ||
| // Connect other routes. | ||
| $routes->scope('/backend', function ($routes) { | ||
| $routes->loadPlugin('ContactManager'); | ||
| }); | ||
| }); | ||
|
|
||
| The above would result in URLs like ``/backend/contact_manager/contacts``. | ||
|
|
||
| .. versionadded:: 3.5.0 | ||
| ``RouteBuilder::loadPlugin()`` was added in 3.5.0 | ||
|
|
||
| Plugin Controllers | ||
| ================== | ||
|
|
||
|
|
@@ -319,34 +369,6 @@ Also make the ``AppController`` if you don't have one already:: | |
| A plugin's ``AppController`` can hold controller logic common to all controllers | ||
| in a plugin but is not required if you don't want to use one. | ||
|
|
||
| Before you can access your controllers, you'll need to ensure the plugin is | ||
| loaded and the plugin routes are loaded. In your **config/bootstrap.php** add | ||
| the following:: | ||
|
|
||
| Plugin::load('ContactManager', ['routes' => true]); | ||
|
|
||
| If you are using ``Plugin::loadAll()`` ensure that routes are loaded:: | ||
|
|
||
| Plugin::loadAll(['routes' => true]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, we do not recommend this anymore?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hate loadAll() personally.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine with me, as it is mentioned in other places on that page, anyway. |
||
|
|
||
| Then create the ContactManager plugin routes. Put the following into | ||
| **plugins/ContactManager/config/routes.php**:: | ||
|
|
||
| <?php | ||
| use Cake\Routing\Route\DashedRoute; | ||
| use Cake\Routing\Router; | ||
|
|
||
| Router::plugin( | ||
| 'ContactManager', | ||
| ['path' => '/contact-manager'], | ||
| function ($routes) { | ||
| $routes->fallbacks(DashedRoute::class); | ||
| } | ||
| ); | ||
|
|
||
| The above will connect default routes for you plugin. You can customize this | ||
| file with more specific routes later on. | ||
|
|
||
| If you want to access what we've got going thus far, visit | ||
| ``/contact-manager/contacts``. You should get a "Missing Model" error | ||
| because we don't have a Contact model defined yet. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the deprecation of
Plugin::routes()missing? Or don't we deprecate it now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking we don't deprecate it based on the discussion we had about making plugin routes more complicated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine, but then https://github.com/cakephp/cakephp/pull/10711/files#diff-7997caf44fa801d62e790043f405bd34R332 needs to be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine, but https://github.com/cakephp/cakephp/pull/10711/files#diff-7997caf44fa801d62e790043f405bd34R332 needs to be removed then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Thank you.