Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Create entirely new Controller

Hello, I'm newbie to vanilla forums

but I'm trying to understand the concept of the controllers. For example: http://vanillaforums/ajax/ ...

Is it possible to create a new one? If yes, how?

I'm having a custom theme. I have to create a folder: controllers in it? And add the new controller .php file in it?

Or I have to create a "virtual controller", using magic methods in a custom plugin, as described here: http://docs.vanillaforums.com/developers/plugins/

Thanks in advance!

Comments

  • R_JR_J Ex-Fanboy Munich Admin

    Basically, when you create a public function PluginController_Something_Create($Sender, $Args) {}, that method can be reached as "yourforum.com/plugin/something/argument1/argument2" and $Args holds array(argument1, argument2).

    If you like to access that method under yourforum.com/fancyname you have to create a custom route.

    You can see how all this is done here: http://vanillaforums.org/addon/howtovanillapage-plugin

  • personally I prefer to extend the vanillaController

    you wouldn't typically have a controller just for ajax, the concept of controller method is they could return a range of deliver type and methods.

    If you set ->Data that could be used for json. Having said that I have implemented these methods a number of ways.

    grep is your friend.

  • hgtonighthgtonight ∞ · New Moderator

    I want to tack on and say you could extend the rootController and dispatch from there.

    public function rootController_sweetNothing_handler($sender, $args) {
        return $this->Dispatch($sender, $args);
    }
    
    public function controller_index($sender, $args) {
        $sender->SetData('Success', true);
        $sender->SetData('Args', $args);
        $sender->RenderData();
    }
    

    This has the added benefit of not requiring a custom route to be at the "top level".

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • R_JR_J Ex-Fanboy Munich Admin

    @CnControllER said:
    I'm having a custom theme. I have to create a folder: controllers in it? And add the new controller .php file in it?

    That part of the question hasn't been answered yet...

    Create a file called class.yourthemenamethemehooks.php like that:

    <?php defined('APPLICATION') or die;
    
    class YourThemeNameThemeHooks implements Gdn_IPlugin {
        public function setup() {
        }
    
        // put here hgtonights code
        // ...
    }
    
  • hgtonighthgtonight ∞ · New Moderator

    @R_J said:

    Create a file called class.yourthemenamethemehooks.php like that:

    <?php defined('APPLICATION') or die;
    
    class YourThemeNameThemeHooks implements Gdn_IPlugin {
        public function setup() {
        }
    
        // put here hgtonights code
        // ...
    }
    

    For my code to work, you would have to use:

    class YourThemeNameThemeHooks extends Gdn_Plugin {
    

    Or write your own dispatch method.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • BleistivtBleistivt Moderator

    Note that you need to initialize the HeadModule yourself when using the rootController:

    $sender->Head = new HeadModule($sender);
    
  • Oh, how many have become bald due to scratching their head about sweetNothing

    public function rootController_sweetNothing_handler($sender, $args) {
        return $this->Dispatch($sender, $args);
    }
    

    Surely there is something to be put in its place, no?

  • R_JR_J Ex-Fanboy Munich Admin

    You will need "create" instead of "handler"

  • edited September 2019

    I have this but no dice, I suspect it is because it is a themehook ...

    I am missing something basic.

    Not sure how to really test if it is working

    class MyThemeHooks extends Gdn_Plugin {
      public function rootController_MyTheme_create($sender, $args) {
        //die('ppp');
        //$sender->Head = new HeadModule($sender);
        return $this->Dispatch($sender, $args);
      }
    
    
      public function controller_index($sender, $args) {
        $sender->SetData('Success', true);
        $sender->SetData('Args', $args);
        $sender->RenderData();
      }
    


  • Aha, I found some info that is very promising that I am going to play with.

    \vanilla-3.2\applications\dashboard\controllers\class.rootcontroller.php

     * This is a placeholder controller that allows plugins to have methods off of the root of the site.
     *
     * If you want to take advantage of this then do the following:
     *  1. Create a method named <code>public function rootController_MyMethod_Create($Sender, $Args)</code>.
     *  2. Program your method just like any other created controller method.
     *  3. When you browse to <code>/mymethod</code> your method will be called.
    
  • edited September 2019

    Confirmed!

    Does not work from a themehook, for a strange reason. But works in a plugin nicely.

    For another person, my code is

    class MyPlugin extends Gdn_Plugin {
      public function rootController_NewBoss_create($sender, $args) {
         return $this->Dispatch($sender, $args);
      }
    
      public function controller_index($sender, $args) {
        // die('debug');
        //$sender->Head = new HeadModule($sender);
        $sender->SetData('Success', true);
        $sender->SetData('Args', $args);
        $sender->RenderData();
      }
    

    Accessed via example.com/forum/newboss

    My plan is to set this as route destination for specified Discussions and do some imaginative rerouting for some super efficient clean urls

Sign In or Register to comment.