Skip to content

Commit

Permalink
Merge pull request #2248 from dreamsxin/tag_1.3.2
Browse files Browse the repository at this point in the history
Fix #2002 Tag::linkTo() to allow the addition of query string parameters
  • Loading branch information
Phalcon committed Apr 28, 2014
2 parents 844347b + 177eb09 commit a3bb614
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
11 changes: 9 additions & 2 deletions ext/mvc/url.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ PHP_METHOD(Phalcon_Mvc_Url, getBasePath){
*
* @param string|array $uri
* @param array|object args Optional arguments to be appended to the query string
* @param bool|null $local
* @return string
*/
PHP_METHOD(Phalcon_Mvc_Url, get){
Expand All @@ -295,16 +296,22 @@ PHP_METHOD(Phalcon_Mvc_Url, get){
zval *service, *route_name, *route = NULL, *exception_message;
zval *pattern = NULL, *paths = NULL, *processed_uri, **args = NULL, *query_string;
zval *matched, *regexp;
zval **z_local = NULL;
int local = 1;

phalcon_fetch_params_ex(0, 2, &uri, &args);
phalcon_fetch_params_ex(0, 3, &uri, &args, &z_local);

PHALCON_MM_GROW();

if (!uri) {
uri = &PHALCON_GLOBAL(z_null);
}
else if (Z_TYPE_PP(uri) == IS_STRING && strstr(Z_STRVAL_PP(uri), "://")) {
else if (z_local && Z_TYPE_PP(z_local) != IS_NULL) {
if (!zend_is_true(*z_local)) {
local = 0;
}
}
else if (Z_TYPE_PP(uri) == IS_STRING && strstr(Z_STRVAL_PP(uri), ":")) {
PHALCON_INIT_VAR(matched);
PHALCON_INIT_VAR(regexp);
ZVAL_STRING(regexp, "/^[^:\\/?#]++:/", 1);
Expand Down
1 change: 1 addition & 0 deletions ext/mvc/urlinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_urlinterface_get, 0, 0, 0)
ZEND_ARG_INFO(0, uri)
ZEND_ARG_INFO(0, args)
ZEND_ARG_INFO(0, local)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_urlinterface_path, 0, 0, 0)
Expand Down
28 changes: 16 additions & 12 deletions ext/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ PHP_METHOD(Phalcon_Tag, linkTo){

zval *parameters, *text = NULL, *local = NULL, *params = NULL;
zval *action, *url = NULL, *internal_url = NULL, *link_text, *z_local;
zval *code;
zval *code, *query;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -774,18 +774,20 @@ PHP_METHOD(Phalcon_Tag, linkTo){
phalcon_array_unset_string(&params, SS("local"), 0);
} else {
PHALCON_INIT_VAR(z_local);
ZVAL_TRUE(z_local);
ZVAL_NULL(z_local);
}

if (zend_is_true(z_local) || Z_TYPE_P(params) == IS_ARRAY) {
PHALCON_CALL_SELF(&url, "geturlservice");

PHALCON_CALL_METHOD(&internal_url, url, "get", action);
phalcon_array_update_string(&params, SL("href"), internal_url, PH_COPY);

if (phalcon_array_isset_string_fetch(&query, params, SS("query"))) {
phalcon_array_unset_string(&params, SS("query"), 0);
} else {
phalcon_array_update_string(&params, SL("href"), action, PH_COPY);
PHALCON_INIT_VAR(query);
ZVAL_NULL(query);
}

PHALCON_CALL_SELF(&url, "geturlservice");
PHALCON_CALL_METHOD(&internal_url, url, "get", action, query, z_local);
phalcon_array_update_string(&params, SL("href"), internal_url, PH_COPY);

PHALCON_INIT_VAR(code);
ZVAL_STRING(code, "<a", 1);

Expand Down Expand Up @@ -1703,6 +1705,7 @@ PHP_METHOD(Phalcon_Tag, stylesheetLink){

zval *parameters = NULL, *local = NULL, *params = NULL, *first_param;
zval *url = NULL, *url_href, *href = NULL, *code, *doctype, *z_local;
zval *rel;

PHALCON_MM_GROW();

Expand Down Expand Up @@ -1761,10 +1764,11 @@ PHP_METHOD(Phalcon_Tag, stylesheetLink){

PHALCON_INIT_VAR(code);

if (!phalcon_array_isset_string(params, SS("ref"))) {
ZVAL_STRING(code, "<link rel=\"stylesheet\"", 1);
if (phalcon_array_isset_string_fetch(&rel, params, SS("rel"))) {
phalcon_array_unset_string(&params, SS("rel"), PH_SEPARATE);
PHALCON_CONCAT_SVS(code, "<link rel=\"", rel, "\"");
} else {
ZVAL_STRING(code, "<link", 1);
ZVAL_STRING(code, "<link rel=\"stylesheet\"", 1);
}

phalcon_tag_render_attributes(code, params TSRMLS_CC);
Expand Down
10 changes: 10 additions & 0 deletions unit-tests/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,14 @@ public function testIssue2142()
$html = \Phalcon\Tag::stylesheetLink(array('css/phalcon.css', 'rel' => 'stylesheet/less'));
$this->assertEquals($html, '<link rel="stylesheet/less" type="text/css" href="/css/phalcon.css" />'.PHP_EOL);
}

public function testIssue2002()
{
$di = new Phalcon\DI\FactoryDefault();
$di->getshared('url')->setBaseUri('/');
\Phalcon\Tag::setDI($di);

$html = Phalcon\Tag::linkTo(array('signup/register', 'Register Here!', 'class' => 'btn-primary', 'query' => array('from' => 'github', 'token' => '123456')));
$this->assertEquals($html, '<a href="/signup/register?from=github&amp;token=123456" class="btn-primary">Register Here!</a>');
}
}

0 comments on commit a3bb614

Please sign in to comment.