programing

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

copysource 2023. 1. 24. 10:06
반응형

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

다음 URL이 있습니다.

이러한 URL에서 컨트롤러 이름, 액션 이름을 가져오는 방법.코드아이그니터 신참입니다.이 정보를 얻을 수 있는 도우미 기능이 있습니까?

예:

$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;

코드 시그니터 사용자 가이드 참조

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

속성 포함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

Last segment of URL will always be the action. Please get like this:

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

// fecth class the class in controller $this->router->fetch_method();

// method

controller class is not working any functions.

so I recommend to you use the following scripts

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];
}

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

반응형