Routes

Routing lets you create your own URL paths, based on the path you can load a closure or a controller.

Routing Set-up

Namespaces are included in all classes now. A namespace is like a layer, adding a namespace to a class means there can be multiple classes with the same name as long as each class is in a different namespace.

With routes the namespace is Routing\Router:: followed by the method call, typing out the namespace every time is long winded, thankfully short cuts can be created by creating an alias:

use Routing\Router;

By using the use keyword Routing\Router, it can be referenced as Router.

To define a route, call the static name Router:: followed by either a post or a get ('any' can also be used to match both post and get requests) to match the HTTP action. Next, set the path to match and call a closure or a controller.

Router::any('', 'closure or controller');

Closures

A closure is a function without a name, they are useful when you only need simple logic for a route, to use a closure first call Router:: then set the URL pattern you want to match against, followed by a function.

Router::get('simple', function() {
//do something simple
});

Controllers and Models can also be used in a closure by instantiating the root controller.

$c = new \App\Core\Controller();
$m = new \App\Models\Users(); $m->getUsers();

Having said that it's best to use a controller, if you need access to a model.

Closures are convenient but can soon become messy.

Controllers

To call a route to a controller, instead of typing a function you can enter a string. In the string type the namespace of the controller (App/Controllers if located in the root of the controllers folder) then the controller name. Finally, specify what method of that class you wish to load. They are dictated by an '@'symbol.

For example, to have a controller called Users (in the root of the controllers folder) and to load ausersList method, you would use the following:

Router::get('users', 'App\Controllers\Users@usersList');

The above would call the Users controller and the userList method when /users is located in the URL, via a get request.

Routes can respond to both GET and POST requests.

To use a post route:

Router::post('blogsave', 'App\Controllers\Blog@savePost');

To respond to either a post or get request, use any:

Router::any('blogsave', 'App\Controllers\Blog@savePost');

Groups

Group routes are new to 3.0. Routes can now be placed in a group, which allows all routes within the group to inherit the group name.

Router::group('admin', function() {
Router::any('add', 'App\Controllers\Demo@cool');
Router::any('settings', 'App\Controllers\Demo@nice');
});

Is the equivalent to

Router::any('admin/add', 'App\Controllers\Admin@add');
Router::any('admin/settings', 'App\Controllers\Admin@settings');

Group Prefixes and Namespaces

The Router::group() can also accept an array as the first parameter and permit commands like:

Router::group(['prefix' => 'admin', 'namespace' => 'App\Controllers\Admin'], function() {
Router::match('get', 'users', 'Users@index');
Router::match('get', 'users/create', 'Users@create');
Router::match('post', 'users', 'Users@store');
Router::match('get', 'users/(:any)', 'Users@show');
Router::match('get', 'users/(:any)/edit', 'Users@edit');
Router::match(['put', 'patch'], 'users/(:any)', 'Users@update');
Router::match('delete', 'users/(:any)', 'Users@destroy');
});

Where the prefix admin will turn the route users/create into admin/users/create and the namespaceApp\Controllers\Admin will prepend onto Users@create, turning intoApp\Controllers\Admin\Users@create

Router::resource()

The Router::resource() method introduces the ability to write the group of resourceful routes, with the following specifications:

HTTP Method Route Controller Method
GET /photo index
GET /photo/create create
POST /photo store
GET /photo/(:any) show
GET /photo/(:any)/edit edit
PUT/PATCH /photo/(:any) update
DELETE /photo/(:any) destroy

The previous code snippet can now be written as:

Router::group(['prefix' => 'admin', 'namespace' => 'App\Controllers\Admin'], function() {
Router::resource('users', 'Users');
Router::resource('categories', 'Categories');
Router::resource('articles', 'Articles');
});

OR

Router::resource('admin/users', 'App\Controllers\Admin\Users');
Router::resource('admin/categories', 'App\Controllers\Admin\Categories');
Router::resource('admin/articles', 'App\Controllers\Admin\Articles');

Routing Filters

Routes can also use filters to dynamically pass values to the controller / closure, there are 3 filters:

  1. (:any) any - can use characters or numbers
  2. (:num) num - can only use numbers
  3. (:all) all - will accept everything including any slash paths

To use a filter place the filter inside parenthesis and use a colon inside route path.

Router::get('blog/(:any)', 'App\Controllers\Blog@post');

Would get past to app/Controllers/Blog.php anything after blog/ will be passed to post method.

public function post($slug)
{
// Some code ...
}

Optional Parameters

New to 3.0 is allowing filters to be optional

Filters which are written like (:any) are required to match the route but writing a filter as (/(:any))makes it optional.

This route supplied with Nova has one filter that is required then a further 3 optional filters. Multiple filters should be inside the first parenthesis.

Router::any('admin/(:any)(/(:any)(/(:any)(/(:any))))', 'App\Controllers\Demo@test');

Full Example

use Routing\Router;

//define routes
Router::get('', 'App\Controllers\Welcome@index'); //call a controller in called users inside a admin folder inside the controllers folder
Router::('admin/users', 'App\Controllers\Admin\Users@list');

Routes的更多相关文章

  1. routes.rb和link_to的一些规则

    rails文档中描述了一个知识,link_to方法用于产生链接,但链接是根据routes.rb中的路由规则来产生的.这又分为面向资源和非面向资源两种产生链接的方法.比如 routes.rb文件中有两条 ...

  2. Python requests 为pfsense 添加Routes

    # !/usr/bin/python 2 # -*- coding: utf-8 -*- __author__ = "Evilxr" import requests ips = o ...

  3. Rails ---> routes.rb 详解

    理解路由的目的 看懂routes.rb文件中的代码 使用经典的hash风格或者现在比较流行的Restful风格构造你自己的路径 断定一个路径会映射到哪一个controller和action 路由的双重 ...

  4. [转]学习Nop中Routes的使用

    本文转自:http://www.cnblogs.com/miku/archive/2012/09/27/2706276.html 1. 映射路由 大型MVC项目为了扩展性,可维护性不能像一般项目在Gl ...

  5. play HTTP路由 http://play-framework.herokuapp.com/zh/routes#syntax

    HTTP路由 HTTP路由(译者注:Play的路径映射机制)组件负责将HTTP请求交给对应的action(一个控制器Controller的公共静态方法)处理. 对于MVC框架来说,一个HTTP请求可以 ...

  6. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

  7. Camel routes in Spring config file

    The normal spring bean definition configuration file, the xsi:schemaLocation only has two: beans and ...

  8. Howto add permanent static routes in Ubuntu

    Static routing is the term used to refer to the manual method used to set up routing. An administrat ...

  9. URAL 1137 Bus Routes(欧拉回路路径)

    1137. Bus Routes Time limit: 1.0 secondMemory limit: 64 MB Several bus routes were in the city of Fi ...

  10. rails里routes配置文件里的resources和resource的区别

    抄自 http://stackoverflow.com/questions/11356146/difference-between-resource-and-resources-in-rails-ro ...

随机推荐

  1. jQuery修改操作css属性实现方法

    在jquery中我们要动态的修改css属性我们只要使用css()方法就可以实现了,下面我来给各位同学详细介绍介绍. css()方法在使用上具有多样性,我们先来了解css()方法基本知识. css() ...

  2. 从linux启动到rootfs的挂载分析

    简单的来说,根文件系统包括虚拟根文件系统和真实根文件系统.在Kernel启动的初始阶段,首先去创建虚拟的根文件系统,接下来再去调用do_mount来加载真正的文件系统,并将根文件系统切换到真正的文件系 ...

  3. POJ 1039 Pipe

    题意:一根管子,中间有一些拐点,给出拐点的上坐标,下坐标为上坐标的纵坐标减1,管子不能透过光线也不能折射光线,问光线能射到最远的点的横坐标. 解法:光线射到最远处的时候一定最少经过两个拐点,枚举每两个 ...

  4. android中GridView关于间距的属性值介绍

    android:columnWidth  设置列的宽度.关联的方法为:setColumnWidth(int)  stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充空间 ...

  5. HDU 5628 Clarke and math Dirichlet卷积+快速幂

    题意:bc round 72 中文题面 分析(官方题解): 如果学过Dirichlet卷积的话知道这玩意就是g(n)=(f*1^k)(n), 由于有结合律,所以我们快速幂一下1^k就行了. 当然,强行 ...

  6. QT中使用 slot 传递 opencv 中得Mat对象以及 使用多线程集成开源代码。

    关于 slot传递 Mat 对象 以前一直是使用 Qtimer 定时器,设定超时后读取 dialog 对象的 Mat成员实现在 UI 里显示图像,发现这样对以后集成其他面向过程的代码增加了复杂度. 所 ...

  7. 发布代码小助手V2.1发布了——Code2HTML工具

    设计起源: 新浪博客似乎没有插入代码的功能,所以不得不用打空格的方法格式化代码.而且没法显示行号. 描述: 发布代码小助手用python和Tkinter开发,可以在任何常见操作系统上运行.主要用于在不 ...

  8. Struts2 url传递中文出现乱码

    项目所有的编码都改为了utf-8.在tomcat的 server.xml中修改下面这段 <Connector port="8080" protocol="HTTP/ ...

  9. 你所不知道的五件事情--java.util.concurrent(第一部分)

                                                                这是Ted Neward在IBM developerWorks中5 things ...

  10. 多个分布式系统如何共享使用一个固定公网IP

    传统的做法,一个分布式业务系统就有一个中间件,一个中间件需要使用至少一个固定公网IP,这样的话,多个业务系统就需要使用多个固定公网IP. 大家知道,固定公网IP价格可是不菲的.能不能让多个分布式业务系 ...