Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to get controller name in views
  • I would like to know how to display conditional html in view template, depending on controller called.
    Something like this:
    <?php if ($class == 'users'): ?>
    html code blah blah
    <?php endif; ?> Can anyone point me in the right direction?
    Thanks
  • \Request::active()->controller
    
  • Yay, thank you! Really appreciate it!
  • Sorry for using this thread, but i think it's related. In the latest fuel, Request::active->controller returns full class name with namespace. I think that is redundant, because we have Request::active->controller_instance property. It would be more consistent to get Request::active->controller like Request::active->action as simple "path/segment" string.
  • As the property name indicates, it contains the name of the controller (as in the class name). It includes the namespace, because you need that to uniquely identify the controller. \Controller_Name is different from \Module\Controller_Name. For the same reason, the action property contains the name of the action called (without the 'action_' prefix), and the method property contains the complete method name, with the 'action_' prefix. If you need a URI segment, use the uri property (which is an instance of the URI class) of the Request object, and the segment methods it provides.
  • While I agree that it could be confusing to have similar values in controller for controllers and controllers in modules, BUT we have:
    1. Request::active()->controller_instance property which we may check with instanceof
    2. Request::active()->module So why we need full namespaced class name in Request::active()->controller property? Except that it's used in request class to call the action from that controller which is internal fuelphp problem? ;)
  • Except that it's used in request class to call the action from that controller which is internal fuelphp problem?
    Actually the primary reason for the existance of the properties is internal, that they're also available is secundairy.
    So why we need full namespaced class name in Request::active()->controller property?
    Because that's the controller, it's not called "controller_segment" it's called controller. And why "we" need it? Because that's where the Request::execute() method takes its cues from. I'll repeat: that's the primary reason for the existance of these properties - that they're available t you is still secundairy.
  • Ok, I see your sarcasm, but still you write framework not for your self, but for other developers, and as one of them I pointed that it would useful to "me" to have controller name somewhere, like it used to be. Anyway thanks for the FW, it's great.
  • You have the controller name available. \Namespace\Controller_Name IS the controller name. What you want is the URI segment. You get that using \Request::active()->uri->segments(). If you want to extract the 'Name' part of the controller name, use
    echo substr(\Inflector::denamespace(\Request::active()->controller), 11);
    
  • May be I don't understand something with \Uri::segment,s but You can't tell which segment referring to controller name?!
  • I wasn't being sarcastic, I may have doubled down a bit but how the framework works is important. But also one thing you seem to miss is that segments don't necessarily match the module/controller/action, when using routing it may get to a whole different point than originally requested. This is why module = module dirname, controller = controller classname and action = method name (though without the action_ prefix as that may not be used when a Controller::router() method is present).
    These are a technical description of the Request, not of the URI. And that's why they don't match those.
  • Exactly that why I don't understand how to use segments to find out module/controller/action name wisely.
    Anyway thanks. P.S. Of course that isn't a problem. It's just it looked shorter in v1.0 :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion