1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

ControllerPublic or ViewPublic ?

Discussion in 'XenForo Development Discussions' started by Mikey, May 27, 2011.

  1. Mikey

    Mikey Well-Known Member

    So, if I am coding an image hosting add on (just as an example (I am not coding an image host add on)), and i have /imghost as my route, set up for everything with a /ControllerPublic/Index.php

    For the index I have an "actionIndex" function in my ControllerPublic.. This works fine as it returns a template to show off..

    but then for example say I wanted to have /imghost/view/awesome.1/ (example again)

    Would I set up an "actionView" function inside the ControllerPublic/Index.php file? Or would I use some other method such as a "ViewPublic" for example?

    I'm just trying to figure out what the best layout to do something like that would be.. or more importantly: what is the right way to do this?
  2. Mikey

    Mikey Well-Known Member

    Well, I figured I'd try something like actionView($id). When I come to visit /bla/view/1 in the browser, it says that "The controller Bla_ControllerPublic_Index does not define an action called View1."
  3. Bob

    Bob Well-Known Member

    the action is always last.. so if your route is /bla and you add an action like actionView then it would be /bla/mikey.1/view
    Mikey likes this.
  4. Kier

    Kier XenForo Developer Staff Member

    It is possible to do this, but you'll need to write your own route matching and link building methods to override the standard router, as the standard approach is /content-type/content/action, rather than /content-type/action/content.
  5. Kier

    Kier XenForo Developer Staff Member

    In answer to your first question, you would create actionView() within your ControllerPublic - nothing you have described here belongs in a ViewPublic.

    However, I would recommend a different approach to simply adding actionView(), as the URL it will produce is overly complex. Instead, I would create a second Route_Prefix such as 'xyz-images' and then have a separate ControllerPublic for that prefix, in which actionIndex() acts as your viewer, so that you end up with the simpler URL of /xyz-images/123. You can implement your actions for working with single images into the same controller, so you get /xyz-images/123/edit and /xyz-images/123/delete etc.

    You can reserve your original route prefix for actions that do not deal with single existing images, such as listing categories or creating a new image.
    Mikey likes this.
  6. Mikey

    Mikey Well-Known Member

    Hmm. Okay well I tried with that way, simply adding a second route prefix& controllerpublic, but couldn't get far with it..

    How would I go about having it the same way as say, the threads on xenforo. Where you have in the url, /threads/title.id (an example) - how is that done? I've tried to discern this via looking through /library/XenForo/ControllerPublic/Thread.php alongside it's route and model, but can't seem to get it down to an exact science yet.. I do note that they use one ControllerPublic file with the actions defined inside there..

    Anyway, if any light could be shed I would be extremely grateful :D
  7. Kier

    Kier XenForo Developer Staff Member

    /threads/title.id is achieved by having the view action for threads be actionIndex() within the XenForo_ControllerPublic_Thread controller, and the use of resolveActionWithIntegerParam() in the router.
    Mikey likes this.

Share This Page