FuelPHP series (2) ------ route routing

 

In FuelPHP, it can be accessed through /controller_name/function_name by default, or through custom routes.

Routes are configured in the /fuel/app/config/routes.php file.

 

First, the simplest routing settings, in the form of key-value pairs.

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

  The key name is the content entered in the URL, and the corresponding value is the method in the requested controller.

2. Add some rules to the routing

:any Can match any character, but cannot be empty
:everything matches any character
:segment matches a part of the URL, which can be any character,
:num match any number
:alpha match greek letters
:alnum matches any letter and number
  return array(
      'article/(:any)'          => 'article/indexxxxx', 
      'article(:everything)'    => 'article/add',
      '(:segment)/article'      => 'test/article',
    '(:segment)article' => 'test1/article',   '(\d{2})/item' => 'site2/item',   );
    'blog/:year/:month/:id' => 'blog/entry',

3. According to different request methods, there are GET, POST, DELETE and so on to route the URL to the controller.

  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)),
  );

  This can also be used in conjunction with the rules above.

Fourth, set up a route with a name

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

  In the static page, you can use the route name to realize the click jump

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

5. The Route class in the core directory

  1.  add( $path , $options = null , $prepend = false , $case_sensitive = null )  to add a route

    The method of the controller the $path route points to

    Parameters in the $options route

    $prepend true route prepares already loaded routes

    $case_sensitive is case sensitive

  Router::add('this/that', 'something/else');  // similar to a simple key-value pair in one

  2.  get( $name , $named_params = array ())  Get the route through the defined route name

    $name the defined route name

    $named_params route parameters

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

  // Based on the routes defined above, the following will return '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'),
  );

  // Based on the routes defined above, the following will return '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 )  delete the defined route

    $path route URL

    $case_sensitive is case sensitive

  // delete route 'this/that' 
  Router::delete('this/that' );

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

  // Set routing case sensitive 
  Router::delete('this/that', true );

  // Delete all routes of the module controller 
  Router::delete('module/controller(:any)');

 

return array(
    'about'   => 'site/about',
    'contact' => 'contact/form',
    'admin'   => 'admin/login',
);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324986057&siteId=291194637