用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5881|回复: 10
收起左侧

[版本 4.x] CI4,如何跳转页面,用redirect()不行了。

[复制链接]
发表于 2018-10-16 15:12:14 | 显示全部楼层 |阅读模式
计划判断是否有登录,没有登录就跳转到登录界面;
先不管判断是否登录了,达成跳转先;

按CI3的经验 redirect('user/login'); 就可以了。
但是不生效,报错误。

TypeError
[size=1.2]Argument 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
 楼主| 发表于 2018-10-16 15:22:39 | 显示全部楼层
算了还是用 html的方式吧;
PHP复制代码
 
    echo '需要登录...';
    echo '<meta http-equiv="refresh" content="1;url='.base_Url().'user/login">';
 
复制代码
发表于 2018-10-17 08:47:00 | 显示全部楼层
本帖最后由 梦想杀死 于 2018-10-17 08:48 编辑
PHP复制代码
return redirect()->route('admin/login/index');
复制代码
多看文档:Docs » General Topics » Global Functions and Constants
发表于 2018-10-17 14:20:52 | 显示全部楼层
header 也可以吧
 楼主| 发表于 2018-10-17 17:07:31 | 显示全部楼层

header 单独的页面可以,但如果用ajax加载就不行。
 楼主| 发表于 2018-10-17 17:09:12 | 显示全部楼层
梦想杀死 发表于 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-18 10:39:56 | 显示全部楼层
冰辉 发表于 2018-10-17 17:07
header 单独的页面可以,但如果用ajax加载就不行。

ajax 的 话 可以返回url 在ajax 结束时用js跳转 ...
发表于 2018-12-17 10:57:10 | 显示全部楼层
怎么样 解决了没? 遇到了同样的问题
发表于 2019-5-29 09:43:17 | 显示全部楼层
如果用 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');

发表于 2019-5-29 09:44:41 | 显示全部楼层
补充:使用 return redirect()->to('user/login');  ,可以不用配置route

本版积分规则