Latest 0.1.0
Homepage https://github.com/wenfzhao/SimpleRouter
License MIT
Platforms ios 8.0, requires ARC
Authors

A Simple Laravel/Lumen Inspired Url-Based Router Library With Middleware Support Written Entirely in Swift

Installation

CocoaPods

You can install SimpleRouter via CocoaPods by adding it to your Podfile:

use_frameworks!

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

pod 'SimpleRouter', '~> 0.1.0'

And run pod install.

Usage

Routing handler

Define a route:

let router = Router.sharedInstance
router.map("/album/:albumId", handler: { (request: RouteReqest) -> (RouteReqest) in
    if let albumId = request.getParam("albumId") {
        //display album here
    }
    return request
})

Initialize a route request:

Router.sharedInstance.routeURL("/album/123?name=hello&year=2016") 
// The handler function will be called with parameters ["albumId": "123", "name": "hell", "year": "2016"].

Named Routes

Define a named route:

let router = Router.sharedInstance
router.map("/album/:albumId", name: "albumDetail", handler: { (request: RouteReqest) -> (RouteReqest) in
    if let albumId = request.getParam("albumId") {
        //display album here
    }
    return request
})

Get the named route url from the router:

let url = Router.sharedInstance.getRouteURL("albumDetail", parameters: ["albumId": "123"]) 
// url = "/album/123"

Middlewares

There are two types of Middlewares – Before Middleware and After Middleware. Before Middleware is executed before hitting the route handler. After Middleware is executed after the route handler.

Create an After middleware:

class StatsMiddleware: Middleware {

    func handle(inRequest: RouteRequest, closure: MiddlewareClosure) -> RouteRequest {
        let request = closure(inRequest)

        //record stats here
        return request
    }

}

Create a Before middleware:

class BeforeMiddleware: Middleware {

    func handle(inRequest: RouteRequest, closure: MiddlewareClosure) -> RouteRequest {
        //put logics executed before the route handler
        //inRequest.setParam("pageId", "5")

        return closure(inRequest)
    }

}

Adding middlewares to route:

let middlewares = [BeforeMiddleware(), AfterMiddleware()]
let router = Router.sharedInstance
router.map("/album/:albumId", handler: { (request: RouteReqest) -> (RouteReqest) in
    if let albumId = request.getParam("albumId") {
        //display album here
    }
    return request
}).withMiddlewares(middlewares)

Deep Linking to App

Install the Test app:
Open simplerouter://album/2 in safari

Latest podspec

{
    "name": "SimpleRouter",
    "version": "0.1.0",
    "summary": "A Simple Laravel/Lumen Inspired Url-Based Router Library With Middleware Support Written Entirely in Swift",
    "description": "This pod provides the ability to use url based routing in app. It's inspired by Laravel/Lumen with middleware support.",
    "homepage": "https://github.com/wenfzhao/SimpleRouter",
    "license": "MIT",
    "authors": {
        "Wen Feng Zhao": "[email protected]"
    },
    "source": {
        "git": "https://github.com/wenfzhao/SimpleRouter.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Source/**/*"
}

Pin It on Pinterest

Share This