Modifying Built-in Controller

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
Hello,

I was wondering what is the best practice if I need to modify a Built-in controller.  In my case I need to modify the Home Controller so that I can pass a model to the Index method.  I know any of the built in views can be overridden by putting them into the Themes/DefaultClean/Views folder.  I tried creating a Controllers folder inside there to override the Home controller but it throws a compile error because the controller is defined twice.  Is there a way to override a controller or what is the best practice for modifying a controller so that its upgrade proof?

Thanks,

Doug
10 years ago
finalthrill wrote:
Hello,

I was wondering what is the best practice if I need to modify a Built-in controller.  In my case I need to modify the Home Controller so that I can pass a model to the Index method.  I know any of the built in views can be overridden by putting them into the Themes/DefaultClean/Views folder.  I tried creating a Controllers folder inside there to override the Home controller but it throws a compile error because the controller is defined twice.  Is there a way to override a controller or what is the best practice for modifying a controller so that its upgrade proof?

Thanks,

Doug


I usually just do a comment on new things added. And with source control, the upgrade is relatively easy (although not upgrade-proof).
10 years ago
wooncherk wrote:
Hello,

I was wondering what is the best practice if I need to modify a Built-in controller.  In my case I need to modify the Home Controller so that I can pass a model to the Index method.  I know any of the built in views can be overridden by putting them into the Themes/DefaultClean/Views folder.  I tried creating a Controllers folder inside there to override the Home controller but it throws a compile error because the controller is defined twice.  Is there a way to override a controller or what is the best practice for modifying a controller so that its upgrade proof?

Thanks,

Doug

I usually just do a comment on new things added. And with source control, the upgrade is relatively easy (although not upgrade-proof).



Well I always take advantage of the Partial Classes where possible, and I place my modified code in my custom partial class along with all the notes I need, and where required comment out the code in the original class.

By doing it this way I keep all my code in separate classes which I can cut and paste when the time comes to upgrade.

As has already been said it's not fool proof but the way I do it suits my need to try and keep my changes to the source code in a separate compartment.
10 years ago
Usually I did the following:

1. create a  View with what ever name but often a same name with View you want to change
2. create a RouteProvider class override the old action with a newAction for example a modified Cart page
//Remove old route
routes.Remove(routes["ShoppingCart"]);

//Add new route
routes.MapLocalizedRoute("ShoppingCart",
                            "cart/",
                            new { controller = "ShoppingCart", action = "NewCart" },
                            new[] { "Nop.Web.Controllers" });


3. create a new partial controller, often I put it in /Themes/[theme name folder]/Controllers/nameOfController.cs, then add a NewAction which declare in RouteProvider class. For example:

public partial class ShoppingCart {
    public ActionResult NewCart()
       {
           ...
       }
}


Another approach is put View in theme folder. then clear all content and add @Html.Action("AnotherViewYouWriteInPartialController")
This method help you to avoid editing the original source code. then not worries about an update version of Nop
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.