未重定向到Codeigniter 4中的特定URL [英] Not redirected to specific URL in Codeigniter 4

查看:58
本文介绍了未重定向到Codeigniter 4中的特定URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我每次通过Codeigniter 4控制器的构造函数重定向某些内容时都不起作用?

Why is it that whenever I redirect something through the constructor of my Codeigniter 4 controller is not working?

<?php namespace App\Controllers\Web\Auth;

class Register extends \App\Controllers\BaseController
{
    function __construct()
    {
        if(session('username')){
            return redirect()->to('/dashboard');
        }
    }
    public function index()
    {
        // return view('welcome_message');
    }
}

但是,如果我将其放入索引中,它将按预期工作.

But if I put it inside index it's working as expected.

public function index()
{
        if(session('username')){
            return redirect()->to('/dashboard');
        }
}

问题是,我不想在索引内直接使用它,因为我需要在同一文件的其他方法上使用它.

The thing is, I do not want to use it directly inside index because I it need on the other method of the same file.

推荐答案

根据Codeigniter论坛,您不能再在构造函数中使用 redirect 方法重定向到任何控制器.

As per the Codeigniter forum, you can no longer use the redirect method in the constructor to redirect to any of the controllers.

请参考下面的链接以获取更多信息

Please refer the below link for more information

https://forum.codeigniter.com/thread-74537.html

它明确指出, redirect()将返回一个类实例而不是设置标头,并且您无法在实例化PHP中的另一个类时返回另一个类的实例.

It clearly states that redirect() will return a class instance instead of setting a header and you cannot return an instance of another class while instantiating a different class in PHP.

这就是为什么您不能在构造函数中使用 redirect 方法的原因.

So that's why you can't use redirect method in constructor.

相反,我可以建议您使用 header 方法并重定向到所需的控制器.

Instead, what I can suggest to you is that use the header method and redirect to your desired controller.

<?php namespace App\Controllers\Web\Auth;

class Register extends \App\Controllers\BaseController
{
    function __construct()
    {
        if(session('username')){
            header('Location: /dashboard');
        }
    }
}

如果这不可行或难以实现,您可以按照以下代码进行操作

If that's not feasible or difficult to achieve you can follow the below code

<?php namespace App\Controllers\Web\Auth;

class Register extends \App\Controllers\BaseController
{
    function __construct()
    {
        //call to session exists method
        $this->is_session_available();
    }

    private function is_session_available(){
        if(session('username')){
            return redirect()->to('/dashboard');
        }else{
            return redirect()->to('/login');
        }
    }
}

第二个解决方案将比第一个解决方案更具交互性.并确保该方法是私有的.这样就不应从其他类实例中调用它.

The 2nd solution will be more interactive than the first one. And make sure the method is private. So that it should not be called from other class instances.

社区团队还提供了一种解决方案,可用于研究 controller过滤器.

The community team has also given a solution to look into the controller filter.

https://codeigniter4.github.io/CodeIgniter4/incoming/filters.html

请参考主题.我希望它可以帮助您找到更好的解决方案.

Please refer to the thread. I hope it may help you in finding a better solution.

这篇关于未重定向到Codeigniter 4中的特定URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆