Search code examples
piranha-cms

Piranha CMS on existing MVC 5 site


I am new to Piranha CMS (and in any CMS in general). I have a question about integrating the system to an existing MVC 5 application. Here is what I have done so far:

  • I have followed the steps in the Getting Started section
  • I have created the database and set up the connection string
  • I have created the admin user and can log in to the manager interface

What I am having trouble is understanding how to bring my existing pages into the manager and how to expose them to the users for editing. I did look into the documentation, but could not find anything dedicated to this topic.

Any help is greatly appreciated.

Thank you. Daniel D.


Solution

  • It's really simple getting data from Piranha CMS into your existing application.

    1. Page types

    First of all, take a look at the different page types you need to create (i.e the different kind of page structures you need) and create them, either from the manager interface or by code. You'll find the docs here:

    2. Create your pages

    Next up, just create the pages you need in the manager and add the content. If you want to prohibit the users to add pages, you can always remove the "Add" buttons later, either by injecting CSS into the manager or by customizing it.

    3. Adding the data to your models

    Here you have two options, either you let your model inherit from the PageModel, or your just add a new property with the CMS data to your existing model.

    // With inheritance
    public class MyModel : Piranha.Models.PageModel {
      ...
    }
    
    // With the CMS data in a property
    public class MyModel {
      public Piranha.Models.PageModel CMSData { get; set; }
    }
    

    4. Getting the data

    Now populating the data is just a calling a method. You can either access it by id or by permalink. Let's for example access it by permalink and let's say we have a page with the permalink about-us. Just add the following to your controller:

    // With inheritance
    public class MyController : Controller {
      public ActionResult MyAction() {
        // Get the CMS-data
        var model = Piranha.Models.PageModel.GetByPermalink<MyModel>("about-us");
    
        // Fill the model with your custom data
        ...
      }
    }
    
    // With the CMS data in a property
    public class MyController : Controller {
      public ActionResult MyAction() {
        // Create and fill your custom model
        var model = new MyModel();
        ...
    
        // Get the CMS-data
        var model.CMSData = Piranha.Models.PageModel.GetByPermalink("about-us");
      }
    }
    

    5. Configuration

    Note that if you want your application to control the routing your should configure Piranha CMS to run in passive mode. You can find the docs on configuration here:

    I hope this helps you get started!

    Regards

    /Håkan