문제

이 URL이 있습니다.

이 URL에서 컨트롤러 이름, 작업 이름을 얻는 방법. 저는 Codeigniter Nevbie입니다. 이 정보를 얻을 수있는 도우미 기능이 있습니까?

전:

$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 ()

속성으로 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-> 세그먼트를 사용하는 경우 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 클래스 컨트롤러의 클래스 $ 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];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top