Yii 프레임워크에서 URL 생성 문제 해결

不言
풀어 주다: 2023-04-01 13:40:02
원래의
1250명이 탐색했습니다.

yii 프레임워크의 URL 생성 문제 요약, 도움이 필요한 친구들이 참고할 수 있습니다.

코드는 다음과 같습니다.

<?php echo CHtml::link(&#39;错误链接&#39;,&#39;user/register&#39;)?> 
<?php echo CHtml::link(&#39;正确链接&#39;,array(&#39;user/register&#39;))?>
로그인 후 복사

UrlManager의 구성이 경로 모드로 설정되고 Yii의 기본 구성이 사용된다고 가정합니다.

&#39;urlManager&#39;=>array( 
&#39;urlFormat&#39;=>&#39;path&#39;, 
&#39;rules&#39;=>array( 
&#39;<controller:\w+>/<id:\d+>&#39;=>&#39;<controller>/view&#39;, 
&#39;<controller:\w+>/<action:\w+>/<id:\d+>&#39;=>&#39;<controller>/<action>&#39;, 
&#39;<controller:\w+>/<action:\w+>&#39;=>&#39;<controller>/<action>&#39;, 
), 
),
로그인 후 복사

위의 두 줄의 코드는 어떤 종류의 링크 주소를 생성합니까?
http:///user/register //잘못된 링크
http:///index.php/user/register //링크가 정확함
첫 번째 링크가 잘못되었습니다. 브라우저는 404 오류를 반환합니다. 두 번째 링크는 UserController의 Register 메서드에 액세스합니다. 차이점은 두 번째 링크가 생성될 때 전달하는 매개변수는 배열인 반면 첫 번째 방법은 간단한 문자열이라는 것입니다. Yii는 Url을 처리할 때 간단한 문자열을 발견하면 해당 문자열을 최종 Url로 직접 사용합니다. 배열을 발견하면 컨트롤러의 CreateUrl을 호출하여 Url을 생성합니다. 실제로는 매우 본질적인 차이입니다. 'user/register' 문자열이기도 하지만 첫 번째 문자열은 13자 상대 경로를 나타내고 두 번째 링크는 특별한 의미를 갖는 UserController의 RegisterAction을 나타냅니다.
첨부된 것은 Yii의 Url 처리 방법 NormalizeUrl의 소스 코드입니다.

/** 
* Normalizes the input parameter to be a valid URL. 
* 
* If the input parameter is an empty string, the currently requested URL will be returned. 
* 
* If the input parameter is a non-empty string, it is treated as a valid URL and will 
* be returned without any change. 
* 
* If the input parameter is an array, it is treated as a controller route and a list of 
* GET parameters, and the {@link CController::createUrl} method will be invoked to 
* create a URL. In this case, the first array element refers to the controller route, 
* and the rest key-value pairs refer to the additional GET parameters for the URL. 
* For example, <code>array(&#39;post/list&#39;, &#39;page&#39;=>3)</code> may be used to generate the URL 
* <code>/index.php?r=post/list&page=3</code>. 
* 
* @param mixed $url the parameter to be used to generate a valid URL 
* @return string the normalized URL 
*/ 
public static function normalizeUrl($url) 
{ 
if(is_array($url)) 
{ 
if(isset($url[0])) 
{ 
if(($c=Yii::app()->getController())!==null) 
$url=$c->createUrl($url[0],array_splice($url,1)); 
else 
$url=Yii::app()->createUrl($url[0],array_splice($url,1)); 
} 
else 
$url=&#39;&#39;; 
} 
return $url===&#39;&#39; ? Yii::app()->getRequest()->getUrl() : $url; 
}
로그인 후 복사
위 내용은 모두의 학습에 도움이 되기를 바랍니다.

관련 권장 사항:

yii 페이징 구성 요소 사용에 대해

부트스트랩 페이징 스타일을 사용하는 yii에 대해

Yii2.0 테이블 연관 쿼리 분석

위 내용은 Yii 프레임워크에서 URL 생성 문제 해결의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
url
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!