라우팅 업그레이드

문서

변경된 사항

  • CI4에서 자동 라우팅은 기본적으로 비활성화되어 있습니다.

  • CI4에서 더 안전한 새로운 자동 라우팅(향상)가 도입되었습니다.

  • CI4에서 라우팅은 더 이상 라우트를 배열로 설정하는 방식으로 구성되지 않습니다.

  • CI3의 와일드카드 (:any)는 CI4에서 플레이스홀더 (:segment)가 됩니다. CI4의 (:any)는 여러 세그먼트와 일치합니다. URI 라우팅을 참조하십시오.

업그레이드 가이드

  1. CI3과 동일한 방식으로 자동 라우팅을 사용하는 경우 Auto Routing (Legacy)를 활성화해야 합니다.

  2. 각 라우팅 줄의 구문을 변경하여 app/Config/Routes.php에 추가해야 합니다. 예를 들어:

    • $route['journals'] = 'blogs';$routes->add('journals', 'Blogs::index');로 변경합니다. 이는 Blogs 컨트롤러의 index() 메서드로 매핑됩니다.

    • $route['product/(:any)'] = 'catalog/product_lookup';$routes->add('product/(:segment)', 'Catalog::productLookup');로 변경합니다. (:any)(:segment)으로 교체하는 것을 잊지 마십시오.

    • $route['login/(.+)'] = 'auth/login/$1';$routes->add('login/(.+)', 'Auth::login/$1');

    참고

    하위 호환성을 위해 여기서는 $routes->add()를 사용합니다. 그러나 보안을 위해 $routes->add() 대신 $routes->get()과 같은 HTTP 동사 라우트를 사용하는 것을 강력히 권장합니다.

코드 예제

CodeIgniter 버전 3.x

경로: application/config/routes.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

// ...

$route['posts/index']  = 'posts/index';
$route['teams/create'] = 'teams/create';
$route['teams/update'] = 'teams/update';

$route['posts/create']   = 'posts/create';
$route['posts/update']   = 'posts/update';
$route['drivers/create'] = 'drivers/create';
$route['drivers/update'] = 'drivers/update';
$route['posts/(:any)']   = 'posts/view/$1';

CodeIgniter 버전 4.x

경로: app/Config/Routes.php:

<?php

use CodeIgniter\Router\RouteCollection;

/** @var RouteCollection $routes */
$routes->get('/', 'Home::index');

$routes->add('posts/index', 'Posts::index');
$routes->add('teams/create', 'Teams::create');
$routes->add('teams/update', 'Teams::update');

$routes->add('posts/create', 'Posts::create');
$routes->add('posts/update', 'Posts::update');
$routes->add('drivers/create', 'Drivers::create');
$routes->add('drivers/update', 'Drivers::update');
$routes->add('posts/(:segment)', 'Posts::view/$1');

참고

하위 호환성을 위해 여기서는 $routes->add()를 사용합니다. 그러나 보안을 위해 $routes->add() 대신 $routes->get()과 같은 HTTP 동사 라우트를 사용하는 것을 강력히 권장합니다.