FuelPHP 系列(二) ------ route 路由

FuelPHP 中,默认可以通过 /controller_name/function_name 这种方式来访问,也可以通过自定义路由来访问。

路由配置在 /fuel/app/config/routes.php 文件中。

一、最简单的路由设置,键值对形式。

  return array(
      'article'   => 'article/index',
      'article/add' => 'article/create',
      'article/edit'   => 'article/edit',
  );

  键名是 URL 中输入的内容,对应的值为请求的控制器中的方法。

二、在路由中加入一些规则

:any 可以匹配任意字符,但不能为空
:everything 匹配任何字符
:segment 匹配 URL 的一部分,这一部分可以是任何字符,
:num 匹配任意数字
:alpha 匹配希腊字母
:alnum 匹配任意字母和数字
  return array(
      'article/(:any)'          => 'article/indexxxxx', 
      'article(:everything)'    => 'article/add',
      '(:segment)/article'      => 'test/article',
    '(:segment)article' => 'test1/article',   '(\d{2})/article' => 'site2/article',
    'blog/:year/:month/:id' => 'blog/entry',   );

三、根据不同请求方式,把 URL 路由到控制器的方法,有 GET、POST、DELETE 等。

  return array(
      'blog' => array(array('GET', new Route('blog/all')), array('POST', new Route('blog/create'))),
    'blog/(:any)' => array(array('GET', new Route('blog/show/$1'), true)),
  );

  这个也可以和上面的规则结合使用。

四、设置有名字的路由

  return array(
      'admin/start/overview' => array('admin/overview', 'name' => 'admin_overview'),
  );

  静态页面中,可以用路由 name 实现点击跳转

  echo Html::anchor(Router::get('admin_overview'), 'Overview');
  <a href="{Router::get('admin_overview')}">Overview</a>

五、core 目录下的 Route 类

  1、 add($path, $options = null, $prepend = false, $case_sensitive = null)  添加一条路由

    $path  路由指向的控制器的方法

    $options  路由中的参数

    $prepend  true 路由为已经加载的路由做准备

    $case_sensitive  是否大小写敏感

  Router::add('this/that', 'something/else');  //类似一中的简单键值对

  2、 get($name, $named_params = array())  通过已定义的路由名字,获取路由

    $name  已定义的路由名称

    $named_params  路由参数

  //1、
  return array(
      'thread/(?P<thread_id>\d+?)/post' => array('post', 'name' => 'post'),
  );

  //根据上面定义的路由,下面这些将返回 'thread/1/post':
  echo Router::get('post', array('thread_id' => 1));
  echo Router::get('post', array('$1' => 1));
  echo Router::get('post', array(1));

  //2、
  return array(
      'country/(?P<country>\d+?)/state/(?P<state>\d+?)/location' => array('location', 'name' => 'location'),
  );

  // 根据上面定义的路由,下面这些将返回 'country/japan/state/tokyo/location':
  echo Router::get('location', array('country' => 'japan', 'state' => 'tokyo'));
  echo Router::get('location', array('$1' => 'japan', '$2' => 'tokyo'));
  echo Router::get('location', array('japan', 'tokyo'));
  echo Router::get('location', array('country' => 'japan', 'tokyo'));
  echo Router::get('location', array('$1' => 'japan', 'tokyo'));

  3、 delete($path, $case_sensitive = null)  删除已定义的路由

    $path  路由 URL

    $case_sensitive  是否大小写敏感

  // 删除路由 'this/that'
  Router::delete('this/that');

  // 删除路由 'this/that' 和'some/other'
  Router::delete(array('this/that', 'some/other'));

  // 设置路由区分大小写
  Router::delete('this/that', true);

  // 删除 module controller 的所有路由
  Router::delete('module/controller(:any)');
return array(
    'about'   => 'site/about',
    'contact' => 'contact/form',
    'admin'   => 'admin/login',
);

猜你喜欢

转载自www.cnblogs.com/rendd/p/8964806.html