Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Extend & replace module controller and view
  • Hello,
    I've been building a user module and now I want to use it in an actual project.
    However in the project as any other real world project users are not just a login name and a password. Users have addresses, orders and other relations.
    These additional things should be visible on login, user dashboard, user profile page - logic that I had already done in my user module. Is there a way to override the users controller so that I can attach the additional logic about addresses and orders?
    I would have imagined there's a way to be done just as I can extend and replace core classes
    Is there a way to override the module views?
  • Hey antitoxic, just wondering how you were able to reroute from the module to your app controller. I have the same problem and I can't seem to reroute to my app's User Controller instead of the one in my module.
  • antitoxic wrote on Wednesday 18th of January 2012:
    Well cheers anyway.
    But notes like "That is what extending does, yes." are no way to attract developers.
    I might have misunderstood the question. Re-reading your question you ask "I have a controller A, I extend it using B extends A, and now I want all requests to A go to B"? Then the answer is still no, from the Frameworks point of view it's a different controller. It doesn't use reflection to see if secretly it was an extension of another controller. What it inherit's completely depends on the code in the controller. Fuel doesn't enforce a particular way of coding, so if your views depend on the controller class name, than inheritance probably doesn't work. If you do something else, it probably will.
  • zejesago wrote on Wednesday 4th of July 2012:
    Hey antitoxic, just wondering how you were able to reroute from the module to your app controller. I have the same problem and I can't seem to reroute to my app's User Controller instead of the one in my module.
    'module/controller/method' => 'controller/method', either in your app of in your module routes file.
  • Harro Verton wrote on Wednesday 4th of July 2012:
    zejesago wrote on Wednesday 4th of July 2012:
    Hey antitoxic, just wondering how you were able to reroute from the module to your app controller. I have the same problem and I can't seem to reroute to my app's User Controller instead of the one in my module.
    'module/controller/method' => 'controller/method', either in your app of in your module routes file.

    I read your reply to antitoxic. I was actually trying to do the same thing; route (module) 'users/login' => 'users/login' (app). Apparently that doesn't work; too bad. Is there another way to do this; without touching the module?
  • Routing logic says that if the first segment is a module, it will continue with the module, and will only fall back to the app if no match for the request is found in the module. With identical names, you can not force it to load an app controller over a module controller.
  • What do you mean by "the user controller". In FuelPHP there is no such thing. If you're referring to the Auth package, I use that for authentication and authorisation only. The users table should have a model like all other tables, and you can use that to interact with the users table, based on the user id you get back from your authentication process. It will also allow you to model crud operations on the users table without having to worry about Auth.
  • Hi WanWizard,
    I'm not referring to the Auth package. I have already implemented a module called "Users" which makes use of the Auth package. The main controller in the users module has actions like login and user dashboard.
    Currently in my application I also want to add addresses and other related information.
    Can I do this without altering the modules's code? By that I mean can I extend & replace the main controller from the Users module with one in my application controllers. i.e.
    User_Controller extends \Users\Main_Controller {
    ...
    }
    

    Is possible that Fuel will recognize User_Controller as a class overriding \Users\Main_Controller and will execute User_Controller actions instead of \Users\Main_Controller actions when a request to the user module is made?
  • It's standard PHP to extend classes like this, so yes. In case \Users refers to a module called 'users', the module needs to be loaded (either via Fuel::add_module or via always_load in the config) to allow FuelPHP to find your Main_Controller.
  • But this would only result into a new controller that extends the module controller. It wouldn't inherit the views. Also the routing (like /users/login) in the Users module will still point to the controller in the module.
    I'll have to re-route them. Is there another way in which the routes will recognize the extended class and route to it?
  • That is what extending does, yes. If you want to route to a different controller, you'll have to modify the route.
  • Well cheers anyway.
    But notes like "That is what extending does, yes." are no way to attract developers.

Howdy, Stranger!

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

In this Discussion