CI4,如何跳转页面,用redirect()不行了。
计划判断是否有登录,没有登录就跳转到登录界面;先不管判断是否登录了,达成跳转先;
按CI3的经验 redirect('user/login'); 就可以了。
但是不生效,报错误。
TypeErrorArgument 1 passed to CodeIgniter\HTTP\Response::setStatusCode() must be of the type integer, null given, called in D:\CodeIgniter\system\HTTP\Response.php on line 757 算了还是用 html的方式吧;
echo '需要登录...';
echo '<meta http-equiv="refresh" content="1;url='.base_Url().'user/login">';
本帖最后由 梦想杀死 于 2018-10-17 08:48 编辑
return redirect()->route('admin/login/index');多看文档:Docs » General Topics » Global Functions and Constants
header 也可以吧 连普科技 发表于 2018-10-17 14:20
header 也可以吧
header 单独的页面可以,但如果用ajax加载就不行。 梦想杀死 发表于 2018-10-17 08:47
多看文档:Docs » General Topics » Global Functions and Constants
嗯嗯;不行呢;
return redirect()->route('user/login');
报:
D:\CodeIgniter\system\HTTP\RedirectResponse.php(87): CodeIgniter\HTTP\Exceptions\HTTPException::forInvalidRedirectRoute('') 冰辉 发表于 2018-10-17 17:07
header 单独的页面可以,但如果用ajax加载就不行。
ajax 的 话 可以返回url 在ajax 结束时用js跳转 ...
怎么样 解决了没? 遇到了同样的问题 如果用 return redirect()->route('user/login');需要在 app/config/Routes.php中 添加类似配置 $routes->get('user/login', 'User::login');
或者使用原来CI3中的redirect,只是CI4改为了return redirect()->to('user/login');如果没去掉index.php,需要 return redirect()->to('/index.php/user/login');
CI4相比以前的版本,改动很大,也希望大家多交流。
关于redirect,CI4中说明文档中描述如下:
https://codeigniter4.github.io/CodeIgniter4/general/common_functions.html?highlight=redirect#redirect
redirect(string $uri)
Parameters:
$uri (string) – The URI to redirect the user to.
Returns a RedirectResponse instance allowing you to easily create redirects:
// Go back to the previous page
return redirect()->back();
// Go to specific UI
return redirect()->to('/admin');
// Go to a named/reverse-routed URI
return redirect()->route('named_route');
// Keep the old input values upon redirect so they can be used by the `old()` function
return redirect()->back()->withInput();
// Set a flash message
return redirect()->back()->with('foo', 'message');
When passing a URI into the function, it is treated as a reverse-route request, not a relative/full URI, treating it the same as using redirect()->route():
// Go to a named/reverse-routed URI
return redirect('named_route');
补充:使用 return redirect()->to('user/login');,可以不用配置route
页:
[1]
2