Custom Login / Registration Look & Feel

Questions about how to change the look and feel of various areas of your Zuluru site
Post Reply
jafo
Posts: 32
Joined: Thu Dec 06, 2018 10:18 am

Custom Login / Registration Look & Feel

Post by jafo »

Hi,

One of the things we are looking to do, is deep link into an event. I think i have figured out some of that but one thing we would like to do is change the look and feel on the login / registration pages. I found the Template/Users/login.ctp file but clearly there is an entire theme being applied to it. Do you have some directions or docs (might be just in the cake stuff that I haven't found yet) to help me customize the look and feel of those pages only OR let me "create a custom page" that I direct users to?

Hope that makes sense...

Thanks,
Brian
GregS
Site Admin
Posts: 240
Joined: Thu Jan 06, 2011 4:58 pm

Re: Custom Login / Registration Look & Feel

Post by GregS »

There is a section about Themes in the readme, that should get you on the right path. If you run into specific issues, let me know.
jafo
Posts: 32
Joined: Thu Dec 06, 2018 10:18 am

Re: Custom Login / Registration Look & Feel

Post by jafo »

First, as I'm still learning CakePHP, I am really impressed with what all it can do quickly.

Second, here is the challenge at this point:
- I can create a plugin and make a theme which affects everyone by adding a default.ctp into /plugins/MyTheme/src/Template/Layout. NOTE: I altered the app_local.php as noted in the readme
- What I can't figure out how to do, is theme ONLY the login.ctp & create_account.ctp located in /src/Template/Users/

Thoughts on how to proceed:
- I could create the exact same layout as what you have now into my theme/plugin, BUT put code logic in it to see the request is for the login page and then use a different file.
- I could modify the User controller to specify the theme but then I think i'm breaking the whole point of how CakePHP is supposed to work.

Neither seems right but it is what I have found so far. In the theme and plugin manuals, it looked like I could replace functionality of another plugin, but that didn't seem to fit to what I was trying to do. Let me know your thoughts.

~Brian
GregS
Site Admin
Posts: 240
Joined: Thu Jan 06, 2011 4:58 pm

Re: Custom Login / Registration Look & Feel

Post by GregS »

Ah, interesting! I'm not sure off the top of my head how to do this. Plugins are excellent for replacing things, but not always so good for extending. What I should probably do is add a callback method that will be used when loading a theme plugin. You could then implement a callback that would register an event listener which would be called before the view is rendered; that listener would check the controller and action and change the layout as required. Sounds complicated, but it's really not too bad. I won't have a chance to do this for a few days, as I'm in the middle of another update. If you're an experienced programmer and just new to CakePHP, you could look at my existing InitializationListener (under src/Event), make your own based on that, and load it manually (for now) in config/bootstrap.php.
jafo
Posts: 32
Joined: Thu Dec 06, 2018 10:18 am

Re: Custom Login / Registration Look & Feel

Post by jafo »

Hi Greg,

Great suggestion on using your listener. I hacked yours up a bit for now until the new method works (sorry :) ).

Code: Select all

	public function beforeRender(Event $event) {
		// Set the theme, if any
		
		//\Cake\Log\Log::write('error', $event->subject);
		$event_info = $event->subject->request->url;
		\Cake\Log\Log::write('error', $event_info);

		if ($event_info == 'users/login') {
			\Cake\Log\Log::write('error','Found User Login');
			Plugin::load('LoginTheme');
			$event->subject()->viewBuilder()->theme('LoginTheme');
		}
		else {
			\Cake\Log\Log::write('error','Not user login: ' . $event_info);
			$theme = Configure::read('App.theme');
			if (!empty($theme)) {
				Plugin::load($theme);
				// Assumption here is that the subject is always a controller
				$event->subject()->viewBuilder()->theme($theme);
			}
		}
	}
NOTE: no judgement on my php skills please, they are a tad rusty. :)
GregS
Site Admin
Posts: 240
Joined: Thu Jan 06, 2011 4:58 pm

Re: Custom Login / Registration Look & Feel

Post by GregS »

Just looking into the same thing for another project today, and learned that you can change the layout from inside a view. So, you might just need to:
- create your plugin folder
- make your own src/Template/Users/login.ctp
- make your own src/Template/Layout/login.ctp
- somewhere in your login view, include

Code: Select all

$this->layout = 'login';
No need for a listener at all! Easy peasy.
Post Reply