当前位置:网站首页>Pecan - route
Pecan - route
2022-07-03 19:14:00 【Procedural ape treading on colorful auspicious clouds】
List of articles
object-dispatch
Pecan Use object-dispatch The routing policy will HTTP The request is mapped to a controller.
This strategy uses / Split into lists , Then from the root controller Start looking for the corresponding layer by layer controller.
Example :
Online store pair /catalog/books/bestsellers The requests are divided into catalog,books and bestsellers. Next ,Pecan Will find on the root controller catalog. And then in catalog Object books, In the end in books Find bestsellers Method .
from pecan import expose
class BooksController(object):
@expose()
def index(self):
return "Welcome to book section."
@expose()
def bestsellers(self):
return "We have 5 books in the top 10."
class CatalogController(object):
@expose()
def index(self):
return "Welcome to the catalog."
books = BooksController()
class RootController(object):
@expose()
def index(self):
return "Welcome to store.example.com!"
@expose()
def hours(self):
return "Open 24/7 on the web."
catalog = CatalogController()
Sometimes , Standard object dispatch routing is not sufficient to URL Route to the controller correctly .Pecan Several methods are provided to simplify the object dispatch system , To deal with... With more control url, Including special _lookup()、_default() and _route() Method . These methods can be defined on the controller object to handle all or part of URL Provide additional flexibility .
_lookup
_lookup() Special methods provide a way to deal with URL Part of , Then return a new controller object to route to the rest .
_lookup() Method can accept one or more parameters , I.e. to be handled URL Path segment ( Split into /)._lookup() You should also accept variable location parameters that represent the rest of the path , And should include any part of the path that it does not handle in the return value . The following example uses a *remainder list , This list will be passed to the returning controller when the object scheduling algorithm continues .
In addition to being used to dynamically create controllers , When no other controller method matches URL And there's no _default() When the method is used ,_lookup() Called as a last resort .
Example :
HTTP GET /8/name The request will return primary_key == 8 The name of the student .
from pecan import expose, abort
from somelib import get_student_by_name
class StudentController(object):
def __init__(self, student):
self.student = student
@expose()
def name(self):
return self.student.name
class RootController(object):
@expose()
def _lookup(self, primary_key, *remainder):
student = get_student_by_primary_key(primary_key)
if student:
return StudentController(student), remainder
else:
abort(404)
_default
When the request path does not match the corresponding method ,_default Will be called .
Example :
/spanish Will be routed to RootController._default()
from pecan import expose
class RootController(object):
@expose()
def english(self):
return 'hello'
@expose()
def french(self):
return 'bonjour'
@expose()
def _default(self):
return 'I cannot say hello in that language'
_route
_route() Method allows the controller to completely cover Pecan The routing mechanism of .Pecan Self use _route() Method to realize its RestController. If you want to Pecan Design an alternative routing system , Then define a definition _route() The basic controller class of the method will give you full control .
边栏推荐
- Does SQL always report foreign key errors when creating tables?
- Why should the gradient be manually cleared before back propagation in pytorch?
- Summary of composition materials for 2020 high-frequency examination center of educational resources
- leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
- 硬盘监控和分析工具:Smartctl
- EGO Planner代码解析bspline_optimizer部分(2)
- 2022.02.11
- Help change the socket position of PCB part
- Find the median of two positive arrays
- Integrated easy to pay secondary domain name distribution system
猜你喜欢

Day18 - basis of interface testing

Ego planner code parsing Bspline_ Optimizer section (3)

Integrated easy to pay secondary domain name distribution system

组策略中开机脚本与登录脚本所使用的用户身份

【光学】基于matlab介电常数计算【含Matlab源码 1926期】

The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking

【学术相关】顶级论文创新点怎么找?中国高校首次获CVPR最佳学生论文奖有感...

2022.2.14 Li Kou - daily question - single element in an ordered array

Free year-end report summary template Welfare Collection

User identity used by startup script and login script in group policy
随机推荐
Which do MySQL and Oracle learn?
硬盘监控和分析工具:Smartctl
Free hand account sharing in September - [cream Nebula]
Work Measurement - 1
Streaming media server (16) -- figure out the difference between live broadcast and on-demand
Record the errors reported when running fluent in the simulator
Think of new ways
Simulation scheduling problem of SystemVerilog (1)
为什么要做特征的归一化/标准化?
Analysis of dart JSON encoder and decoder
The way to treat feelings
Max of PHP FPM_ Some misunderstandings of children
Yolov3 network model building
【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
我們做了一個智能零售結算平臺
High concurrency Architecture - read write separation
Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
EGO Planner代碼解析bspline_optimizer部分(1)
组策略中开机脚本与登录脚本所使用的用户身份
Processing of user input parameters in shell script