programing tip

CodeIgniter : 컨트롤러, 액션, URL 정보를 얻는 방법

itbloger 2020. 7. 16. 19:44
반응형

CodeIgniter : 컨트롤러, 액션, URL 정보를 얻는 방법


다음 URL이 있습니다.

이 URL에서 컨트롤러 이름, 작업 이름을 얻는 방법 저는 CodeIgniter 초보자입니다. 이 정보를 얻는 데 도움이되는 도우미 기능이 있습니까?

전의:

$params = helper_function( current_url() )

$params같은 이되는

array (
  'controller' => 'system/settings', 
  'action' => 'edit', 
  '...'=>'...'
)

URI 클래스를 사용할 수 있습니다 .

$this->uri->segment(n); // n=1 for controller, n=2 for method, etc

또한 다음과 같은 작업을 들었지만 현재 테스트 할 수 없습니다.

$this->router->fetch_class();
$this->router->fetch_method();

URI 세그먼트를 사용하는 대신 다음을 수행해야합니다.

$this->router->fetch_class(); // class = controller
$this->router->fetch_method();

이렇게하면 라우팅 된 URL 뒤에 있거나 하위 도메인 등에서라도 항상 올바른 값을 사용하고 있음을 알 수 있습니다


이 메소드는 더 이상 사용되지 않습니다.

$this->router->fetch_class();
$this->router->fetch_method();

대신 속성에 액세스 할 수 있습니다.

$this->router->class;
$this->router->method;

codeigniter 사용 설명서 참조

URI 라우팅 메소드 fetch_directory (), fetch_class (), fetch_method ()

properties CI_Router::$directory를 사용 CI_Router::$class하고 CI_Router::$method공개하고 각각 fetch_*()의 속성이 더 이상 속성을 반환하기 위해 다른 작업을 수행하지 않으면 속성을 유지하는 것이 의미가 없습니다.

그것들은 모두 문서화되지 않은 내부 방법이지만, 만약을 대비하여 이전 버전과의 호환성을 유지하기 위해 지금은 더 이상 사용하지 않기로 결정했습니다. 일부 사용자가이를 활용 한 경우 이제 속성에 액세스 할 수 있습니다.

$this->router->directory;
$this->router->class;
$this->router->method;

또 다른 방법

$this->router->class

또한

$this -> router -> fetch_module(); //Module Name if you are using HMVC Component

최신 정보

답변은 2015 년에 추가되었으며 다음 방법은 더 이상 사용되지 않습니다.

$this->router->fetch_class();  in favour of  $this->router->class; 
$this->router->fetch_method(); in favour of  $this->router->method;

안녕 당신은 다음과 같은 접근 방식을 사용해야합니다

$this->router->fetch_class(); // class = controller
$this->router->fetch_method(); // action

이 목적을 위해 이것을 사용하려면 후크를 확장해야 CI_Controller하며 매력처럼 작동하므로 URI 세그먼트를 사용하지 않아야합니다


$ this-> uri-> segment를 사용하는 경우 URL 다시 쓰기 규칙이 변경되면 세그먼트 이름 일치가 손실됩니다.


수업이나 도서관 어디에서나이 강령을 사용하십시오

    $current_url =& get_instance(); //  get a reference to CodeIgniter
    $current_url->router->fetch_class(); // for Class name or controller
    $current_url->router->fetch_method(); // for method name

URL의 마지막 세그먼트는 항상 작업입니다. 다음과 같이하십시오 :

$this->uri->segment('last_segment');

$this->router->fetch_class(); 

// 컨트롤러의 클래스를 fecth class $ this-> router-> fetch_method ();

// 방법


컨트롤러 클래스가 작동하지 않습니다.

다음 스크립트를 사용하는 것이 좋습니다.

global $argv;

if(is_array($argv)){
    $action = $argv[1];
    $method = $argv[2];
}else{
    $request_uri = $_SERVER['REQUEST_URI'];
    $pattern = "/.*?\/index\.php\/(.*?)\/(.*?)$/";
    preg_match($pattern, $request_uri, $params);
    $action = $params[1];
    $method = $params[2];
}

참고 URL : https://stackoverflow.com/questions/2062086/codeigniter-how-to-get-controller-action-url-information

반응형