2010-11-19 2 views
1

경로 작업의 하이픈을 밑줄로 변경하려면 Kohana 3와 어떻게해야합니까?

Route::set('crud', 'staff/<controller>(/<action>(/<id>))', array(
      'controller' => '(activities|users|default-emails)', 
      'action' => '(new|view|modify|delete)', 
      'id' => '\d+' 
    ))->defaults(array(
     'directory' => 'staff', 
     'action' => 'view' 
    )); 

default-emails

분명하지 않습니다 존재하지 수 action_default-emails() 방법을 실행하려고 ... bootstrap.php이 경로를 고려하십시오.

내부적으로 하이픈을 밑줄로 표시하려면 Kohana의 어떤 부분을 확장해야합니까?

내가 이렇게하면 _- 구분 된 경로를 통해 액세스 할 수 있는지 걱정해야합니까?

감사합니다.

답변

3

간단한 당신이 다음의 각 kohana 버전이 패치를해야 할 것을, 당신이 이해 Kohana_Request::execute()@1112

$class->getMethod('action_'.$action)->invokeArgs($controller, $this->_params); 

변화

$class->getMethod('action_'.str_replace('-', '_', $action))->invokeArgs($controller, $this->_params); 

에 그러나 해킹하는 것입니다.

더 무해한는 확장 할 수 Kohana_Route::matches()

class Route extends Kohana_Route 
{ 
    public function matches($uri) 
    { 
     $matches = parent::matches($uri); 
     if (isset($matches['action'])) 
     { 
      $matches['action'] = str_replace('-', '_', $matches['action']; 
     } 
     return $matches; 
    } 
} 

확인하지 못했지만, 나는 그것이 작동합니다 내기.

+0

감사의 말 - 나중에 기능 요청을 제출할 수 있습니다. – alex

+0

@alex : 거절 당함, 100 % ;-) – zerkms

+1

@alex : 또 다른 방법은 "Kohana_Controller"를 확장하고 그런 경우를 처리하는'__call()'을 만드는 것입니다. – zerkms

4

적절한 밑줄 방법에 하이픈 작업을 라우팅하는 Kohana 3.2에 대한 나의 솔루션 :이 코드

if($key == 'action') 
{ 
    $params[$key] = str_replace('-','_',$value); 
} 
else 
{ 
    $params[$key] = $value; 
} 

전체 솔루션 삽입
Kohana_Route을 확장하고 :
응용 프로그램/클래스에 파일 route.php 만들기를/

<?php defined('SYSPATH') or die('No direct script access.'); 

class Route extends Kohana_Route 
{ 
    public function matches($uri) 
    { 
     if ($this->_callback) 
     { 
      $closure = $this->_callback; 
      $params = call_user_func($closure, $uri); 

      if (! is_array($params)) 
       return FALSE; 
     } 
     else 
     { 
      if (! preg_match($this->_route_regex, $uri, $matches)) 
       return FALSE; 

      $params = array(); 
      foreach ($matches as $key => $value) 
      { 
       if (is_int($key)) 
       { 
        // Skip all unnamed keys 
        continue; 
       } 

       // Set the value for all matched keys 
           if($key == 'action') 
           { 
            $params[$key] = str_replace('-','_',$value); 
           } 
           else 
           { 
            $params[$key] = $value; 
           } 
      } 
     } 

     foreach ($this->_defaults as $key => $value) 
     { 
      if (! isset($params[$key]) OR $params[$key] === '') 
      { 
       // Set default values for any key that was not matched 
       $params[$key] = $value; 
      } 
     } 

     return $params; 
    } 
} 
+0

이것은 Kohana 코어 팀이 제안한 솔루션보다 내 요구 사항에 더 나은 것을 포함하여 지금까지 해본 적이있는 가장 깨끗한 방법입니다 (저는 모든 단일 경로에 정규 표현식이나 콜백을 넣지 않아도된다는 팬이 아닙니다. 확장 클래스로 쉽게 할 수있는 것을 달성하십시오.) Kohana 3.3에서 작동하도록 수정해야했지만 클래스를 확장하고 일치하는 함수를 조정하는 _approach_는 훌륭하게 작동합니다. –

1

zerkms의 방법에 대한 업데이트입니다. Kohana 3.2에서 system/classes/kohana/request/client/internal.php줄 106을 편집해야합니다.

은 교체 :

$action = $request->action(); 

작성자 :

$action = str_replace('-', '_', $request->action()); 

내가 해킹 프레임 워크를 싫어하지만, 여기까지 가장 간단하고 신뢰할 수있는 솔루션입니다. Route 클래스에서 액션의 이름을 변경하면 모든 종류의 문제가 발생할 수 있습니다. 액션은 때때로 my_action (내부적으로), 언젠가는 my-action (링크)으로 호출되기 때문입니다.

3

Kohana 3.3이 나온 이래로이 방법은 더 이상 작동하지 않습니다. 나는 지금까지 나를 위해 일하는 해결책을 발견했다.

3.3으로 업그레이드 할 때 Internal.php 요청 파일을 편집 할 필요가 없습니다. 대신 경로 필터를 만들 수 있습니다. 액션의 하이픈을 밑줄로 바꾸면됩니다.

Route::set('default', '(<controller>(/<action>(/<id>)))') 
    ->filter(function($route, $params, $request) { 
     // Replacing the hyphens for underscores. 
     $params['action'] = str_replace('-', '_', $params['action']); 
     return $params; // Returning an array will replace the parameters. 
    }) 
    ->defaults(array(
     'controller' => 'welcome', 
     'action'  => 'index', 
    )); 

이 방법은 분명히 작동합니다. 그러나 좀 더 살펴보면 디렉토리, 컨트롤러 등에 대해 더 나은 기능을 만들 수 있습니다.

관련 문제