3
votes

I'm very new with orchard.

To learn orchard module development, I followed the documentation and tried to create a commerce module.

The module consists of product part and product type which has product part.

During enable module, it will create admin and home menu for this module, "Commerce" and "Shop" respectively.

My questions are

  • How do I make this module to be home page during enable module. In other word, I want Index method of the module's HomeController handle home url?
  • How do I get Shop menu in front end to be after home menu or register this module to home menu?

I am attaching source code, please download it from the following link

download source code

3
+1 for the first part of your question. And @bertrand has a VERY useful succinct answer!gideon

3 Answers

4
votes

To take over the home page the standard Orchard way is to implement IHomePageProvider.

1
votes

You can, when creating a page as part of migrations.cs in a module, tell the Autoroute part to set your created page's alias as the homepage:

        //create a page page
        var homepage = _contentManager.Create("Page");
        homepage.As<TitlePart>().Title = "My Home";            
        _contentManager.Publish(homepage);

        var homePageArp = homepage.As<AutoroutePart>();
        homePageArp.DisplayAlias = String.Empty;            
        _autorouteService.PublishAlias(homePageArp);

This assumes you're going from a clean instance of Orchard without any prior homepages; if you have an existing homepage, you'll have to regenerate those pages' Aliases as part of your module too. This is how it's done as part of the AutoroutePartHandler in the Orchard.Autoroute project (inside the Publish Alias method):

            // regenerate the alias for the previous home page
            var currentHomePages = _orchardServices.ContentManager.Query<AutoroutePart, AutoroutePartRecord>().Where(x => x.DisplayAlias == "").List();
            foreach (var current in currentHomePages) {
                if (current != null) {
                    current.CustomPattern = String.Empty; // force the regeneration
                    current.DisplayAlias = _autorouteService.Value.GenerateAlias(current);
                }
                _autorouteService.Value.PublishAlias(current);
            }


        _autorouteService.Value.PublishAlias(part);

If you dig through the driver and handler for the autoroute project, you'll learn a lot about the internals; when you tick that "set as homepage" box in the Admin UI, it sets the Path to "/" and then that gets picked up, triggers the old homepage re-wire, clears the "/" path to String.Empty and then publishes that blank alias, giving you a new homepage.

(this is valid as of Orchard 1.6)

0
votes

If your module is to be used by others, then it is better to make a widget which can be added to any layer (the homepage layer for example). That way each user can decide where your module comes into play. If you are using this module for yourself only, then you can just override the default routes (standard mvc functionallity). Look at my ExtendedRegistration module (Routes.cs) to see how it's done.

Here I am overriding the standard Account/Register URL. There should be nothing preventing you from overriding the default HomeController.

public class Routes : IRouteProvider
    {

        public void GetRoutes(ICollection<RouteDescriptor> routes)
        {
            foreach (var routeDescriptor in GetRoutes())
            {
                routes.Add(routeDescriptor);
            }
        }

        public IEnumerable<RouteDescriptor> GetRoutes()
        {
            return new[] {

                    new RouteDescriptor {
                    Priority = 19,
                    Route = new Route(
                        "Users/Account/Register",
                        new RouteValueDictionary {
                            {"area", "itWORKS.ExtendedRegistration"},
                            {"controller", "Account"},
                            {"action", "Register"}
                        },
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "itWORKS.ExtendedRegistration"}
                        },
                        new MvcRouteHandler())
                }
            };
        }
    }