All Projects → sun6boys → CRRouter

sun6boys / CRRouter

Licence: MIT license
A simple and powerful router

Programming Languages

objective c
16641 projects - #2 most used programming language
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to CRRouter

Falco
A functional-first toolkit for building brilliant ASP.NET Core applications using F#.
Stars: ✭ 214 (+296.3%)
Mutual labels:  router, routing
router
Bidirectional Ring router. REST oriented. Rails inspired.
Stars: ✭ 78 (+44.44%)
Mutual labels:  router, routing
Klein.php
A fast & flexible router
Stars: ✭ 2,622 (+4755.56%)
Mutual labels:  router, routing
Routerify
A lightweight, idiomatic, composable and modular router implementation with middleware support for the Rust HTTP library hyper.rs
Stars: ✭ 173 (+220.37%)
Mutual labels:  router, routing
routex.js
🔼 Alternative library to manage dynamic routes in Next.js
Stars: ✭ 38 (-29.63%)
Mutual labels:  router, routing
Next Routes
Universal dynamic routes for Next.js
Stars: ✭ 2,354 (+4259.26%)
Mutual labels:  router, routing
Swiftuirouter
Routing in SwiftUI
Stars: ✭ 242 (+348.15%)
Mutual labels:  router, routing
Redux Tower
Saga powered routing engine for Redux app.
Stars: ✭ 155 (+187.04%)
Mutual labels:  router, routing
journey
A conductor routing helper library
Stars: ✭ 35 (-35.19%)
Mutual labels:  router, routing
es6-router
🌐 Simple client side router built in ES6
Stars: ✭ 16 (-70.37%)
Mutual labels:  router, routing
Flow builder
Flutter Flows made easy! A Flutter package which simplifies flows with a flexible, declarative API.
Stars: ✭ 169 (+212.96%)
Mutual labels:  router, routing
RouteNow
RouteNow is a small fast library ⚡ that will help you in developing a SinglePage Application without any dependencies like jQuery, AngularJs, vue.js or any of those bulky frameworks.
Stars: ✭ 17 (-68.52%)
Mutual labels:  router, routing
Rayo.js
Micro framework for Node.js
Stars: ✭ 170 (+214.81%)
Mutual labels:  router, routing
Ui Router
The de-facto solution to flexible routing with nested views in AngularJS
Stars: ✭ 13,738 (+25340.74%)
Mutual labels:  router, routing
Router
⚡️ A lightning fast HTTP router
Stars: ✭ 158 (+192.59%)
Mutual labels:  router, routing
Router
Router implementation for fasthttp
Stars: ✭ 234 (+333.33%)
Mutual labels:  router, routing
Grip
The microframework for writing powerful web applications.
Stars: ✭ 137 (+153.7%)
Mutual labels:  router, routing
Nextjs Dynamic Routes
[Deprecated] Super simple way to create dynamic routes with Next.js
Stars: ✭ 145 (+168.52%)
Mutual labels:  router, routing
Clevergo
👅 CleverGo is a lightweight, feature rich and high performance HTTP router for Go.
Stars: ✭ 246 (+355.56%)
Mutual labels:  router, routing
neteng-roadmap
Network Engineering at Scale Roadmap/Landscape
Stars: ✭ 53 (-1.85%)
Mutual labels:  router, routing

CRRouter

License MIT

CRRouter是什么

CRRouter是一个利用URL注册,通过URL获取对象或者打开一个页面的模块解耦框架。

CRRouter提供了哪些功能

  • 通过一个URL获取一个对象
  • 通过一个URL打开相应页面
  • 参数验证
  • 参数映射

安装

CocoaPods

  1. 在Podfile中添加 pod "CRRouter".
  2. 输入命令 pod install 或者 pod update.
  3. Import <CRRouter/CRRouter.h>.

如何使用

注册

CRRouter提供了2种注册方式

//第一种注册方式
CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];
[CRRouter registRouteNode:routeNode];
//第二种注册方式
[CRRouter registURLPattern:@"cr://goods/goodsDetail"]

参数映射

有时候我们需要从第三方app中打开我们app,并且进入某个页面。我们需要给他们提供scheme、host、path以及所需的参数,比如cr://goods/goodsDetail?goodsId=12389,但是我们不想告诉第三方页面所需要的真正参数名,所以我们告诉他们通过cr://goods/goodsDetail?p1=12389来打开商品详情页面,而我们真正需要的参数名是goodsId,CRRouter提供了下面的方式来映射参数。

CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];

[routeNode paramsMap:^NSDictionary *(NSDictionary *originParams) {
    NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
    if(originParams[@"p1"]){
        //说明是第三方调用
        temp[@"goodsId"] = originParams[@"p1"];
     }
    [temp addEntriesFromDictionary:originParams];
    return temp;
 }];

So,对于我们内部仍然可以通过cr://goods/goodsDetail?goodsId=12389来调用,而第三方通过cr://goods/goodsDetail?p1=12389也可以调用到相应的模块

参数验证

以上面为例,打开一个商品详情页,一个goodsId是必须的,CRRouter提供相应的参数验证

CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];

[routeNode paramsValidate:^BOOL(NSDictionary *originParams, NSDictionary *routeParams) {
     NSString *goodsId = routeParams[@"goodsId"];
     return goodsId.length > 0;
 }];

如果参数验证失败,将不会继续执行下去。如果实现了参数映射的block,那验证的是映射后的参数

实现注册的URL相应的获取实例操作

CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];

[routeNode objectHandler:^id(NSDictionary *routeParams) {
     CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];
     goodsVC.goodsId = routeParams[@"goodsId"];
     return goodsVC;
 }];

如果实现了参数验证,如果这个block被调用,那肯定是参数验证通过了。

实现注册的URL打开页面的操作

CRRouteNode *routeNode = [CRRouteNode routeNodeWithURLScheme:@"cr" URLHost:@"goods" URLPath:@"/goodsDetail"];

[routeNode openHandler:^(NSDictionary *routeParams) {
     CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];
     goodsVC.goodsId = routeParams[@"goodsId"];
     UINavigationController *navigationVC = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
     [navigationVC pushViewController:goodsVC animated:YES];
 }];

如果实现了参数验证,如果这个block被调用,那肯定是参数验证通过了。

如何调用

通过URL获取一个实例

UIViewController *vc = [CRRouter objectForURL:@"cr://goods/goodsDetail?p1=1"];

如果一些参数不方便通过URL query传递(比如block)可以用下面的方式获取实例

dispatch_block_t completionHandler = ^{
        
};
UIViewController *vc = [CRRouter objectForURL:@"cr://goods/goodsDetail?goodsId=1" withParams:@{@"block" : completionHandler}];

通过URL打开一个页面

[CRRouter openURL:@"cr://goods/goodsDetail?p1=1"];

dispatch_block_t completionHandler = ^{
        
};
[CRRouter openURL:@"cr://goods/goodsDetail?goodsId=1" withParams:@{@"block" : completionHandler}];

支持链式语法

如果你不喜欢上面的调用方式,CRRouter也提供类似ReactiveCocoa那样的调用方式

[[[[[CRRouter registURLPattern:@"cr://goods/goodsDetail"] paramsMap:^NSDictionary *(NSDictionary *originParams) {
        
    NSMutableDictionary *temp = [[NSMutableDictionary alloc] init];
    temp[@"goodsId"] = originParams[@"p1"];
    [temp addEntriesFromDictionary:originParams];
    return temp;
        
}] paramsValidate:^BOOL(NSDictionary *originParams, NSDictionary *routeParams) {
        
    NSString *goodsId = routeParams[@"goodsId"];
    return goodsId.length > 0;
        
}] objectHandler:^id(NSDictionary *routeParams) {
        
    CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];
    goodsVC.goodsId = routeParams[@"goodsId"];
    return goodsVC;
        
}] openHandler:^(NSDictionary *routeParams) {
        
    CRGoodsViewController *goodsVC = [[CRGoodsViewController alloc] init];
    goodsVC.goodsId = routeParams[@"goodsId"];
    UINavigationController *navigationVC = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [navigationVC pushViewController:goodsVC animated:YES];
        
}];

TODO

  1. 添加参数验证
  2. 添加参数映射
  3. 通过URL可以打开一个页面
  4. 通过URL可以获取一个对象
  5. 通过URL可以调用相应Target的Action

如果大家有好的建议也可以通过Issues的方式或者邮件的方式告诉我。

协议

CRRouter被许可在 MIT 协议下使用。查阅 LICENSE 文件来获得更多信息。

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].