Partager via


[Orchard 강좌] 24. 초간단 모듈 제작 : HelloWorld 모듈 만들기

이 내용은 ASP.NET MVP로 활동하고 계신 박용준님이 작성하신, 오픈소스 기반 웹사이트 제작 엔진인 Orchard로 처음 웹사이트를 구축 및 개발하기 위한 시리즈 강좌입니다. 많은 도움 되시길 바랍니다.
==================================================================================================

소개

이번 시간에는 앞서서 계속해서 사용해 본 모듈(Module)을 직접 간단하게나마 만들어보는 시간을 갖도록 하겠습니다.

하지만… 하지만… 말 그대로 “Hello World”를 화면에 출력하는 수준입니다.

아쉽지만, 이번 강좌 시리즈에서는 개발자를 위한 것이기보다는 학생개발자/디자이너/퍼블리셔/관리자를 위해서 포커스를 맞추었습니다.

따라하기

Step 1: HelloWorld 모듈 생성하기

1. WebMatrix로 Modules 폴더를 열어보면 현재 Orchard에서 사용중인 모듈이 폴더 단위로 저장되어져 있음을 알 수 있습니다.

image_thumb.png

2. 새로운 모듈을 제작하기 위해 Orchard.exe 파일을 실행합니다.

image_thumb_1.png

3. Orchard 명령 프롬프트 환경에서 “codegen module HelloWorld” 명령어를 통해서 HelloWorld란 이름의 모듈을 생성합니다.

image_thumb_2.png

4. WebMatrix로 돌아와서 MJodules 폴더를 새로고침하면 아래와 같이 HelloWorld란 폴더가 생성되고 기본 모듈의 폴더 구조를 갖습니다. 사실, 이 폴더구조는 ASP.NET MVC 3의 Areas 기능에서 사용되는 구조입니다.

image_thumb_3.png

Step 2: 모듈 실행을 위한 주요 파일 생성하기

1. 아래 그림처럼 HelloWorld 폴더에 [Class] 파일을 [Routes.cs] 이름으로 생성합니다.

image_thumb_5.png

2. 생성된 Routes.cs 파일을 아래와 같이 작성합니다. 직접 작성하지 말고, 아래 소스코드를 복사해서 붙여넣기하면 됩니다.

image_thumb_6.png

 using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard.Mvc.Routes;

namespace HelloWorld {
    public class Routes : IRouteProvider {
        public void GetRoutes(ICollection<RouteDescriptor> routes) {
            foreach (var routeDescriptor in GetRoutes())
                routes.Add(routeDescriptor);
        }
        // ASP.NET MVC 3 Area
        public IEnumerable<RouteDescriptor> GetRoutes() {
            return new[] {
                new RouteDescriptor {
                    Priority = 5,
                    Route = new Route(
                        "HelloWorld",
                        new RouteValueDictionary {
                            {"area", "HelloWorld"},
                            {"controller", "Home"},
                            {"action", "Index"}
                        },
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "HelloWorld"}
                        },
                        new MvcRouteHandler())
                }
            };
        }
    }
}

3. 이번에는 HelloWorld 폴더 아래의 [Controllers] 폴더에 [Class] 파일을 HomeController.cs로 파일을 생성합니다.

image_thumb_7.png

4.

image_thumb_8.png

 using System.Web.Mvc;
using Orchard.Themes;

namespace HelloWorld.Controllers {
    //[Themed]
    public class HomeController : Controller {
        public ActionResult Index() {
            return View("HelloWorld");
        }
    }
}

5. 다음에 아래 그림과 같이 Views-Home 폴더를 생성하고 HelloWorld.cshtml 파일을 생성하고 아래와 같이 입력합니다.

image_thumb_10.png

 <h3>@T("안녕하세요...")</h3>
<h3>반갑습니다.</h3>

6. WebMatrix에서 마지막으로 HelloWorld 프로젝트 루트에 있는 HelloWorld.csproj 파일을 열고 아래 2개의 영역을 입력합니다.

image_thumb_11.png

 ...
     <Reference Include="System.Xml" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Xml.Linq" />
  </ItemGroup>

<ItemGroup>
  <Compile Include="Routes.cs"/>
  <Compile Include="Controllers\HomeController.cs"/>
</ItemGroup>

  <ItemGroup>

    <Content Include="Views\HelloWorld.cshtml" />

    <Content Include="Web.config" />
    <Content Include="Views\Web.config" />
    <Content Include="Scripts\Web.config" />
...

Step 3: 만들어진 모듈 활성화

1. Orchard.exe 환경으로 들어와서 아래 그림처럼 “feature enable HelloWorld” 명령어를 실행하여 HelloWorld 모듈을 활성화시킵니다.

image_thumb_12.png

2. 홈페이지 메인으로 돌아와서 웹브라우저 주소창에 아래 그림과 같이 /HelloWorld를 요청하면, 간단히 메시지가 출력됩니다.

image_thumb_13.png

3. 만약 상하좌우에 대한 테마를 적용하고자한다면, HomeController.cs 파일로 이동해서 [Themed] 부분을 적용해주면 됩니다.

image_thumb_14.png

 using System.Web.Mvc;
using Orchard.Themes;

namespace HelloWorld.Controllers {
    [Themed]
    public class HomeController : Controller {
        public ActionResult Index() {
            return View("HelloWorld");
        }
    }
}

4. 테마가 적용된 후의 모습입니다.

image_thumb_15.png

5. HelloWorld 모듈의 경로를 [대시보드]-[네비게이션]에서 메인 메뉴로 등록해서 사용 가능합니다.

image_thumb_9.png

마무리

ASP.NET MVC를 사용하는 개발자라면 현재 강좌의 내용이 어렵지 않으리라 봅니다. 하지만, 본 강좌 시리즈는 HTML과 CSS만을 알면 사용 가능하도록 설명하기를 목표로 잡았기에 모듈(Module)을 만드는 과정은 이번 시간에 맛배기로 보여드리고자 합니다.

전문 개발자 분들은 Orchard 사이트(https://www.orchardproject.net)의 Documents를 활용하기 바랍니다.

끝.

참고 자료

관련글