句法 Syntaxphp
参数 Parametersweb
Luthier CI更改CodeIgniter路由的行为:正则表达式
您可使用三种类型的路由:api
AJAX路由进入api.php数组
虽然你能够在 web.php
文件中定义AJAX路由,但最好这样作 api.php
闭包
若是您使用相同的URL和相同的HTTP动词定义两条或更多路线,则第一条路线将被返回ALWAYS
Luthier CI容许您使用动词GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS和TRACE定义HTTP路由:app
若是您使用过Laravel,那么您将知道如何使用Luthier CI,由于它的语法是相同的。这是路线最简单的例子:框架
Route::get('foo', 'bar@baz');
第一条路由是获胜的路由 curl
其中foo是路径的URL,bar @ baz是它所指向的控制器和方法(由@分隔)的名称。经过使用get()您告诉Luthier CI 的方法,该路由将在GET请求下可用。函数
Luthier CI容许您使用动词GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS和TRACE定义HTTP路由:
Route::post('foo', 'bar@baz'); Route::put('foo', 'bar@baz'); Route::patch('foo', 'bar@baz'); Route::delete('foo', 'bar@baz'); Route::head('foo', 'bar@baz'); Route::options('foo', 'bar@baz'); Route::trace('foo', 'bar@baz');
此外,您能够将具备路径属性的数组做为第三个参数传递(稍后解释)。
Route::get('test', 'controller@method', ['prefix' => '...', 'namespace' => '...', (...)] );
要在路由中接受多个HTTP谓词,请使用如下match()方法:
Route::match(['GET', 'POST'], 'path', 'controller@method', [ (...) ]);
namespace属性告诉CodeIgniter控制器所在的子目录。(注意,这不是PHP名称空间,它是目录名称)
// The controller is located in application/controllers/admin/Testcontroller.php Route::get('hello/world', 'testcontroller@index', ['namespace' => 'admin']);
使用该prefix属性为路由添加前缀:
// The URL will be 'admin/hello/world' instead of 'hello/world' Route::get('hello/world', 'testcontroller@index', ['prefix' => 'admin']);
您能够(事实上,这是可取的)为您的路线指定一个名称。这将容许您从其余地方调用它们:
Route::get('company/about_us', 'testcontroller@index')->name('about_us');
要经过它的名称获取路由,请使用该route()函数,其第一个参数是路由的名称,第二个可选参数是具备该路由的参数值的数组。例如,要获取上一个路由,只需写route('about_us'):
// http://example.com/company/about_us <a href="<?= route('about_us');?>">My link!</a>
重复的名称 ( Duplicated names )
您不能使用相同的名称调用两个或多个路由
您可使用该group()方法建立路由组,其中第一个参数是它们共有的前缀,第二个参数是具备子路由的匿名函数:
Route::group('prefix', function(){ Route::get('bar','test@bar'); Route::get('baz','test@baz'); });
此外,能够为路由组分配共同的属性。这是扩展语法的示例:
Route::group('prefix', ['namespace' => 'foo', 'middleware' => ['Admin','IPFilter']], function(){ Route::get('bar','test@bar'); Route::get('baz','test@baz'); });
资源路由容许您在单行上为控制器定义CRUD操做(Create, Read, Update, Delete) 例:
Route::resource('photos','PhotosController');
生产:
[Name] [Path] [Verb] [Controller action] photos.index photos GET PhotosController@index photos.create photos/create GET PhotosController@create photos.store photos POST PhotosController@store photos.show photos/{id} GET PhotosController@show photos.edit photos/{id}/edit GET PhotosController@edit photos.update photos/{id} PUT, PATCH PhotosController@update photos.destroy photos/{id} DELETE PhotosController@destroy
此外,能够建立部分资源路由,传递第三个参数,其中包含要过滤的操做数组:
Route::resource('photos','PhotosController', ['index','edit','update']);
生产:
[Name] [Path] [Verb] [Controller action] photos.index photos GET PhotosController@index photos.edit photos/{id}/edit GET PhotosController@edit photos.update photos/{id} PUT, PATCH PhotosController@update
Luthier CI自动设置使用URL / 和HTTP谓词 GET 定义的任何路由做为默认控制器,可是您可使用 set()
方法和这种特殊语法显式设置它:
// Note that the value is binded to the special 'default_controller' route of CodeIgniter and you must // use the native syntax: Route::set('default_controller', 'welcome/index');
您可使用匿名函数(也称为闭包或lambda函数)而不是指向控制器,例如:
Route::get('foo', function(){ ci()->load->view('some_view'); });
要访问匿名函数中的框架实例,请使用该ci()函数。
能够在路线中定义参数,以便它们能够是动态的。要将参数添加到路径的某个段,请将其括起来{curly brackets}
Route::post('blog/{slug}', 'blog@post');
重复参数 ( Duplicated parameters )
您不能使用相同名称调用两个或多个参数
要建立可选参数,请?在关闭大括号以前添加一个:
Route::put('categories/{primary?}/{secondary?}/{filter?}', 'clients@list');
请注意,在定义第一个可选参数后,如下全部参数必须是可选的。
Routes generated automatically
生成的路由Luthier CI将为您生成全部可选参数的完整路径树,所以您没必要担忧编写除主要路径以外的更多路径。
您能够将参数限制为正则表达式:
// These are the equivalents of (:num) and (:any), respectively: Route::get('cars/{num:id}/{any:registration}', 'CarCatalog@index');
此外,您可使用具备如下{([expr]):[name]}语法的自定义正则表达式:
// This is equivalent to /^(es|en)$/ Route::get('main/{((es|en)):_locale}/about', 'about@index');
您可能须要在一组路由中定义一个参数,而后在全部子路由中均可以使用该参数,而没必要在全部控制器中的全部方法的参数中定义它,这很繁琐。考虑到这一点,Luthier CI提供了所谓的Sticky参数。粘性参数如下划线(_
) 开头并具备一些奇点:
考虑这个例子:
Route::group('shop/{_locale}', function() { Route::get('category/{id}', 'ShopCategory@categoryList')->name('shop.category'); Route::get('product/{id}/details', 'ShopProduct@details')->name('shop.product.details'); });
路由shop.category并shop.product.details共享_locale粘性参数。虽然该参数必须位于URL中,但route()在此上下文中使用该函数时,并不强制它出如今参数值数组中。当您须要连接到当前路径的其余变体时,这尤为有用:
// If the URL is 'shop/en/category/1', {_locale} will be 'en' here: echo route('shop.category', ['id' => 1]); # shop/en/category/1 echo route('shop.category', ['id' => 2]); # shop/en/category/2 echo route('shop.category', ['id' => 3]); # shop/en/category/3 // You can overwrite that value for any other: echo route('shop.category', ['_locale' => 'es', 'id' => 1]); # shop/es/category/1
粘性参数的一个优势是您没必要将它们定义为指向控制器的全部方法的参数。在前面的示例中,在ShopCategory和ShopProduct控制器中,它们的方法将只有一个参数:$id,由于它是路由器提供的惟一参数:
<?php # application/controllers/ShopCategory.php defined('BASEPATH') OR exit('No direct script access allowed'); class ShopCategory extends CI_Controller { // Define the method as categoryList($_locale, $id) will not work: it is // waiting for exactly 1 argument: public function categoryList($id) { } }
<?php # application/controllers/ShopProduct.php defined('BASEPATH') OR exit('No direct script access allowed'); class ShopProduct extends CI_Controller { // Same here: public function details($id) { } }
要获取sticky参数的值,请使用控制器中属性的param()方法route:
<?php # application/controllers/ShopCategory.php defined('BASEPATH') OR exit('No direct script access allowed'); class ShopCategory extends CI_Controller { public function categoryList($id) { $locale = $this->route->param('_locale'); } }