codeigniter 4路由命名路由不生成href
codeigniter 4 routing named routes generates no href
我有一条路线应该为锚标记生成 href,但我没有收到 href:
<a href="" style="color:white !important" class="btn btn-info postlist">Update</a>
我上面的代码是:
data[i]["confirm"] = '<a href="<?=route_to('updatePost', 1) ?>" style="color:white !important" class="btn btn-info postlist">Update</a>';
我的路线是:
//$routes->add('post/(:id)', 'App/Controllers/Post::updatepost/');
$routes->add('post/(:id)', 'Post::updatepost/', ['as' => 'updatePost']);
我期待 this
注意:尝试了未命名和命名方式都没有生成任何 href
The short answer is (:id) isn't supported. It's been deprecated in favour of using (:num)
So the quick fix is to use (:num) instead of (:id)
It is the same.
一个临时修复方法是在确实需要时更改核心文件。
免责声明:强烈建议不要更改核心文件。
这样做需要您自担风险
在文件 /system/Router/RouteCollection.php - 第 117 行
原为:
/**
* Defined placeholders that can be used
* within the
*
* @var array
*/
protected $placeholders = [
'any' => '.*',
'segment' => '[^/]+',
'alphanum' => '[a-zA-Z0-9]+',
'num' => '[0-9]+',
'alpha' => '[a-zA-Z]+',
'hash' => '[^/]+',
];
如果真的需要,可以是:
/**
* Defined placeholders that can be used
* within the
*
* @var array
*/
protected $placeholders = [
'any' => '.*',
'segment' => '[^/]+',
'alphanum' => '[a-zA-Z0-9]+',
'num' => '[0-9]+',
'alpha' => '[a-zA-Z]+',
'hash' => '[^/]+',
'id' => '[0-9]+'
];
更改是添加模仿 'num'.[=13= 的 'id' 条目]
简单地将所有对 (:id) 的引用更改为 (:num)
会更安全
我有一条路线应该为锚标记生成 href,但我没有收到 href:
<a href="" style="color:white !important" class="btn btn-info postlist">Update</a>
我上面的代码是:
data[i]["confirm"] = '<a href="<?=route_to('updatePost', 1) ?>" style="color:white !important" class="btn btn-info postlist">Update</a>';
我的路线是:
//$routes->add('post/(:id)', 'App/Controllers/Post::updatepost/');
$routes->add('post/(:id)', 'Post::updatepost/', ['as' => 'updatePost']);
我期待 this
注意:尝试了未命名和命名方式都没有生成任何 href
The short answer is (:id) isn't supported. It's been deprecated in favour of using (:num)
So the quick fix is to use (:num) instead of (:id)
It is the same.
一个临时修复方法是在确实需要时更改核心文件。
免责声明:强烈建议不要更改核心文件。 这样做需要您自担风险
在文件 /system/Router/RouteCollection.php - 第 117 行
原为:
/**
* Defined placeholders that can be used
* within the
*
* @var array
*/
protected $placeholders = [
'any' => '.*',
'segment' => '[^/]+',
'alphanum' => '[a-zA-Z0-9]+',
'num' => '[0-9]+',
'alpha' => '[a-zA-Z]+',
'hash' => '[^/]+',
];
如果真的需要,可以是:
/**
* Defined placeholders that can be used
* within the
*
* @var array
*/
protected $placeholders = [
'any' => '.*',
'segment' => '[^/]+',
'alphanum' => '[a-zA-Z0-9]+',
'num' => '[0-9]+',
'alpha' => '[a-zA-Z]+',
'hash' => '[^/]+',
'id' => '[0-9]+'
];
更改是添加模仿 'num'.[=13= 的 'id' 条目]
简单地将所有对 (:id) 的引用更改为 (:num)
会更安全