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

A "right way" to extend UsersController, and have the routes point to the extended controller? (latest develop branch) #823

Closed
groovenectar opened this issue Sep 26, 2019 · 8 comments

Comments

@groovenectar
Copy link
Contributor

groovenectar commented Sep 26, 2019

Using the latest develop branch

I did follow the docs here: https://github.com/CakeDC/users/blob/develop/Docs/Documentation/Extending-the-Plugin.md#extending-the-controller

Controller ./src/Controller/UsersController.php:

<?php

namespace App\Controller;

use CakeDC\Users\Controller\Traits\LoginTrait;
use CakeDC\Users\Controller\Traits\RegisterTrait;

class UsersController {
	use LoginTrait;
	use RegisterTrait;
}

Config file ./config/users.php:

<?php

$config = [
	'Users' => [
		'table' => 'Users',
		'controller' => 'Users',
	],
	'Auth' => [
		'AuthenticationComponent' => [
			'load' => false, // Same result using `true` here
			'loginAction' => [
				'plugin' => false,
				'controller' => 'Users',
				'action' => 'login',
				'prefix' => false,
			],
			'logoutRedirect' => [
				'plugin' => false,
				'controller' => 'Users',
				'action' => 'login',
				'prefix' => false,
			],
		],
		'Authenticators' => [
			'Form' => [
				'loginUrl' => [
					'plugin' => false,
					'controller' => 'Users',
					'action' => 'login',
					'prefix' => false,
				]
			],
			'Cookie' => [
				'loginUrl' => [
					'plugin' => false,
					'controller' => 'Users',
					'action' => 'login',
					'prefix' => false,
				]
			],
		],
		'AuthorizationMiddleware' => [
			'unauthorizedHandler' => [
				'url' => [
					'plugin' => false,
					'controller' => 'Users',
					'action' => 'login',
				]
			]
		],
	],
];

return $config;

This results in the core UsersController still being used when accessing /login

If the route is overridden in the app's routes.php (I'm not sure if that is the correct pattern, just one of the possible solutions tried):

Router::connect('/login', ['plugin' => false, 'controller' => 'Users', 'action' => 'login']);

Then this results in a redirect loop...

Doing a dd(Configure::read()); shows that the configuration values are loaded, overriding the default values

@groovenectar groovenectar changed the title A "right way" to extend UsersController, and have the routes point to the extended controller? A "right way" to extend UsersController, and have the routes point to the extended controller? (latest develop branch) Sep 26, 2019
@rochamarcelo
Copy link
Collaborator

Hi,

The documentation needs to be updated.

Flagging this as 'improvement'.

Thanks.

@groovenectar
Copy link
Contributor Author

groovenectar commented Oct 17, 2019

If there's a way to do it that does already work, feel free to point me in the right direction and I'll want to work on updating the documentation with the process

@rochamarcelo
Copy link
Collaborator

rochamarcelo commented Oct 22, 2019

@rochamarcelo
Copy link
Collaborator

Keep in mind that develop branch is not a 'stable' branch, you should use a stable release on production.

@groovenectar
Copy link
Contributor Author

Please check https://github.com/CakeDC/users-example/tree/develop-extend-plugin

Thank you!!! This is probably exactly what I'm looking for :)

Keep in mind that develop branch is not a 'stable' branch, you should use a stable release on production.

Yep! My goal is to help with the develop branch and see the latest middleware pattern out there as well

@rochamarcelo
Copy link
Collaborator

@groovenectar In this case take a look into branch 9.next

@steinkel
Copy link
Member

Thank you for helping us @groovenectar

@groovenectar
Copy link
Contributor Author

groovenectar commented Oct 30, 2019

My pleasure @steinkel @rochamarcelo

Also I found out what was going on here!

In config/permissions.php

The 'plugin' keys must be set to false -- When they're set to CakeDC/Users, that's what is causing the redirect!

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

No branches or pull requests

3 participants