[공지] SMS114.CO.KR 바로가기
www.sms114.co.kr
👉 텔레그램 상담 바로가기

1 분 소요

@route

url 체제를 만들어주는 것.

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

위에서 보듯, defaul_controller는 welcom, 하지만 파일이름은 대문자로 기술되어야 한다.

그래서 Welcome.phpdefault_controller 의 첫 파일인것이다.

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

class Welcome extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/userguide3/general/urls.html
	 */
	public function index()
	{
		$this->load->view('welcome_message');
	}
}

2. codeIgnite 에서 Controller 와 View 주요파일

  • routes.php

    • 코드이그나이트에서 라우팅(Routing) 을 설정하는데 사용됨, 라우팅은 사용자가 입력한 url 을 특정 컨트롤러와 메서드를 매핑하는 역할을 한다.

    • routes 파일의 위치는 ?
      application/config/routes.php
              
      ===========inside file;
      $route['default_controller'] = 'main';
      $route['404_override'] = '';
      $route['translate_uri_dashes'] = FALSE;
          
      컨트롤러 폴더의, Main.php  찾는다.
      
    • routes.php 의 주요 역할

      • url 매핑

        • 사용자가 브라우저에서 입력한 url 을 특정 컨트롤러와 매서드에 매핑함.
          )
                      
          $route['prouduct'] = 'catalog/products';
          

          -위 라우트는 사용자가 http://your-domain.com/products로 요청을 보낼 경우, Catalog 컨트롤러의 products 메서드를 실행하게 만들어 준다.

    • routes.php 의 주요 설정

      • 기본 라우트(default route)

        • CodeIgniter 애플리케이션잉 처음 로드될 때 실행될 기본 컨트롤러와 매서드를 지정
          $route['default_controller'] = 'welcome';
          

          사용자가 아무 url 도 입력하지 않으면 ( http://your-domain.com/), Welcome 컨트롤러의 index 매서드가 실행된다.

      • 404 error 라우트 설정

        • url 이 잘못 입력되었거나 해당 경로에 대한 정의가 없을 때 보여줄 에러 페이지를 설정

        • 예:

          $route['404_override'] = 'errors/page_missing';
          

          잘못된 url 로 접근했을 때, Errors 컨트롤러의 page_missing 메서드가 호출된다.

컨트롤러와 뷰를 설정하는데 있어, routes.php 가 그 처음 로드되는 경로를 설정해준다.

3. 잘못된 파일 로드 시, 즉 404 file Not Found 에 대한 routes.php 설정

routes.php 의 아래 코드를 삽입해 준다.

$route['(:any)'] = 'welcome';

댓글남기기