How do I add controller actions using an add-on? I've written a new controller and added it to the package-info.xml file, but it's as if the controller dispatcher is unaware of it. How do I make it aware so that ?action=blah leads to my action?
There are at least a couple of ways.
The first and most "future-proof" is to name it "properly" and put it in the "correct" place.
For example, if you want to add a non-admin action, let's say something like
index.php?action=myaction you can store the file in
sources/controllers (a.k.a. CONTROLLERDIR), the name of the file should be Myaction.controller.php, and it should be inside a class name
Myaction_Controller that should extend
Action_Controller.
So:
<?php
class Myaction_Controller extends Action_Controller
{
public function action_index()
{
//This method is required and should forward the the default (sub)action.
}
}
Then, for each sub-action (e.g.
index.php?action=myaction;sa=something) you can have a method like
action_something:
<?php
class Myaction_Controller extends Action_Controller
{
public function action_index()
{
//This method is required and should forward the the default (sub)action.
$this->action_something();
}
public function action_something()
{
//This takes care of the sub action "something"
}
}
The other way, is to use the $actionArray and the corresponding hook integrate_actions, but this method should be used only if it is not possible to use the naming pattern (in other words it's there just because some internal actions are not yet mapped to the new way).
The dispatcher supports as well the use of functions instead of classes, but I wouldn't suggest that method, because, as proposed
here (http://www.elkarte.net/community/index.php?topic=2112.0), it may be removed in the "close" future. ;)
Hmmm, I've tried all of that correctly as far as I can tell. Could you check-over my work?
ExtAuth.controller.php
<?php
/**
* @package External Authentication
*
* @author Antony Derham
* @copyright 2014 Antony Derham
*
* @version 1.0
*/
if (!defined('ELK'))
die('No access...');
/**
* ExtAuth_Controller class, deals with authenticating external accounts
*/
class ExtAuth_Controller extends Action_Controller
{
/**
* Entry point in ExtAuth controller
*/
public function action_index()
{
// Only action, try and authenticate
$this->action_extlogin();
}
/**
* Attempt to authenticate them with HybridAuth
*
* What it does:
* - takes the provider ID from GET var and attempts HybridAuth login.
*/
public function action_extlogin()
{
// Include the HybridAuth external libaray and configuration
$config = EXTDIR . '/hybridauth/config.php';
require_once(EXTDIR . '/hybridauth/Hybrid/Auth.php');
I'm calling "index.php?action=extauth", but it presents the board index.
Unix is case sensitive?
Extauth.controller.php
Well that worked perfectly :D Thanks. How does MergeTopics.controller.php and the others get away with camel case then?
That's one of those that still require the $actionArray. O:-)
ETA:
/me feels some awesomeness coming from this question! :D
I've got to admit, it's not easy to tip-toe around the core Elkarte code. I want to try and build the add-on without changing too much, but I might be submitting some PRs.
From what I can work out, the easiest way to enter into registration/login is through the OpenID handling code that already exists.
heh...
I know the feeling.
TBH last time I tried to look into this, the idea that came to my mind was rewrite the registration/authentication/login code entirely, because as it is now, even the OpenID implementation seems hacked into the normal registration so that "it works".
If you have ideas that would improve the situation, feel free to send PRs. And if you feel comfortable, don't be shy on changing large chunks. ;)
Yeah, I did think about rewriting it entirely, but it annoyed me when I was writing my theme how the login page is already duplicated: one template for error and one for no error.
I'll see how it goes integrating it into the OpenID code, and if needs be, I'll make changes where I need.
When I noticed it, it was kind of late (I try not to enter into the themes dir lol) to merge them into one...
It's on the list for 1.1 though.
Okay, that idea sank like a lead balloon xD Back to a separate register function and page in the ExtAuth controller.
After about an hour of steaming before finally realising I hadn't added the correct globals (fml), I've just successfully logged into Elkarte with Twitter.
YAY! :D
Facebook working. Steam working. Google failing. Something on the HybridAuth-side. Google's APIs have gotten really complex.
Google working too now.
It's designed (hopefully) in such a way that you can take existing HybridAuth-compatible provider plugins and just drop them straight into this addon.
Next thing: have the user be able to associate and disassociate providers from the account settings page.
I'll leave registration stuff until last since I can then re-use the subs I create for associating.
Not had to touch any of the core Elkarte code yet :)
For anyone interested in how the Xydre theme handles the external logins
(http://i.imgur.com/ZD8NXBS.png)
WOW! :D
/me has a new hero. nods
That is awesome ant59. Just for that, you should get a raise to ant60! ;)
Make this part of core when/if you can guys!
ROFL @ IchBin :P
I'm having trouble loading the tempalte for the profile page. I'm getting this:
for this:
public function action_profile()
{
loadTemplate('Extauth');
}
I've added the link to the profile menu here:
'extauth' => array(
'label' => 'Connected Accounts',
'file' => 'Extauth.controller.php',
'controller' => 'Extauth_Controller',
'function' => 'action_profile',
'sc' => 'post',
'token' => 'profile-ea%u',
'password' => true,
'permission' => array(
'own' => array('profile_identity_any', 'profile_identity_own'),
'any' => array('profile_identity_any'),
),
),
Not sure what's going wrong. It's as if it's completely ignoring the loadTemplate call.
I have a "Extauth.template.php" file in themes/default/ with a template_main() function that just echoes 'Test'. But it won't show, and instead throws that error.
template_main is "deprecated" in the sense that it's better not to rely on it and always name explicitly the template you want to use using the sub_template index:
$context['sub_template'] = 'my_template';
then, the menu template tries to be smart and sets a "default" sub_template to the name of the sub-action, in that case "action_profile", so in your template file you should have:
function template_action_profile ()
{
}
That should do the trick. ;)
Awesome! It works :D Thank you very much
@emanuele. You're an encyclopedia of Elkarte :P
LOL
For sure I know the bugs I wrote. :P
A little more progress
(http://i.imgur.com/ncL4xfr.png)
Association and disassociation work perfectly on the fly :D
Wonderful, I'd love to see that as part of the ElkArte Core, so where's the pull request? ;)
Haha! I'll release it as an addon first, but I'd have no objection to it being included in the core. I'd prefer it to be optional though, rather than forcibly included for people that don't want to use it as per my suggestion here: http://www.elkarte.net/community/index.php?topic=2155
Got a working registration template. I'll add the provider they're coming from at some point too.
(http://i.imgur.com/fWQwwmS.png)
EDIT: I might integrate the provider into the Register button, as with the login buttons (give it colour, icon, etc...).
I rolled with that idea and got this. Pretty happy with it. Now I've got to write the handling action for submitting the form and either registering them or telling them they screwed up (oh joy).
(http://i.imgur.com/UvSq2s1.png)
I'm wondering whether I should put it all into a single column though. The agreement requires javascript to scale it's height as-is and I can't think of a CSS-only solution.
Very very nice! :D
I think you should go with moving it to a single column. Easier to read, especially if you're looking at it on a smaller screen (mobile).
I should have been clearer. It's responsive and does move to a single column on small screens. But on medium and large screens, I'm wondering between multi or single.
I think a single column would be better too, even on big screens. It is easier to read long texts (and most of the agreements are long) if the space for the text, the rows are wide enough.
That's true, though there are two points to consider:
1) nobody reads agreements (apart maybe two or three exceptions),
2) have to scroll down to find the only buttons you are looking for ("register using ...") may be annoying to the average user (as an example, I find annoying the gmail registration page lol).
3) (heck, I continue messing with numbers LOL) even Amazon has dropped the "I accept the agreement" for a simpler "signing up you accept the [link]terms and conditions[/link] (not that I remember if it ever had a box to tick).
I am wondering whether it's worth removing the checkbox altogether and opt for the "Agree and Register with Facebook" option instead. That saves having to check for it being ticked too. Guess I could submit a PR for core with that option too as I did with the checkbox.
This is how it currently looks across different sized screens:
https://www.youtube.com/watch?v=3Qgb-_FZluM
I've pushed my code to Github. Feel free to check it over. I've got some more work to do on registration still, but everything else works. I'd appreciate it if someone can check over the security side of things and make sure I've not left out any checks.
https://github.com/Ant59/elkarte-external-authentication
Please don't use it yet. I haven't decided on a license and it doesn't currently work fully.
I like it already just by reading this topic. Great job!
Already forked it and will test it.
@emanuele, how should I proceed with this? Would it be preferable:
- As a PR for the core
- As an addon for good
- As an addon for now, possibly to become a core module in the future
I'd vote for "As a PR for the core" -> Developement -> Elk 1.1
If you already have an addon I wouldn't stop you from distributing it! ;D (I would in fact encourage you!)
If you then want to push it to the code... I guess it's fine, 1.1 would be the branch! :D
Any new development on this addon? Will it be released as addon for 1.0? Will it be added to 1.1 branch? Unpatiently waiting... ;D
Me too!
Any news on this? I'd like to have social login on my forum.
Not yet, I think. It most probably will be in 1.1.
Not really unless someone takes care of coding it. ;)
Ok. I really thought it has been PR to 1.1. What about its license? Public?
https://github.com/eurich/elkarte-external-authentication
Problem is: there's no licence file available, The existing files just contain:
/**
* @package External Authentication
*
* @author Antony Derham
* @copyright 2014 Antony Derham
*
* @version 1.0
*/
If feel we need
@Ant59 's permission to adapt the code or we'd have to rewrite the code..