php CodeIgniter - 显示成功或不成功的消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8442539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:43:20  来源:igfitidea点击:

CodeIgniter - displaying success or unsuccess message

phpcodeigniter

提问by BigJobbies

Im wondering if someone could let me know how they handle success / unsuccess messages in CodeIgniter.

我想知道是否有人可以让我知道他们如何处理 CodeIgniter 中的成功/不成功消息。

For example, what im doing at the moment when a user signs up to my site, this is what happens in the controller

例如,当用户注册我的网站时我在做什么,这就是控制器中发生的事情

if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') ))) {
    // Redirect to the successful controller
    redirect( 'create-profile/successful' );
} else {
    // Redirect to the unsuccessful controller
    redirect( 'create-profile/unsuccessful' );
}

Then in the same controller (create-profile), i have 2 methods which are like the following

然后在同一个控制器(创建配置文件)中,我有两种方法,如下所示

function successful()
{
    $data['h1title'] = 'Successful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
}

The problem with this is that i can simply go to site.com/create-profile/successful and it will show the page.

问题在于,我可以简单地访问 site.com/create-profile/successful,它会显示该页面。

If someone could possibly show me a better way of handling this, it would be greatly appreciated.

如果有人可以向我展示处理此问题的更好方法,将不胜感激。

Cheers,

干杯,

回答by duxins

You could set a flashdatabefore the redirection:

您可以在重定向之前设置flashdata

$this->session->set_flashdata('create_profile_successful', $some_data);
redirect( 'create-profile/successful' );

 

 

function successful(){
    if( FALSE == ($data = $this->session->flashdata('create_profile_successful'))){
        redirect('/');  
    }

    $data['h1title'] = 'Successful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
}

回答by Stefan Koenen

is there a reason you don't use this:

你有什么理由不使用这个:

if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') ))) {
    $data['h1title'] = 'Successful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
} else {
    $data['h1title'] = 'Unsuccessful Registration';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('message',$data);
}

greets, stefan

问候,斯蒂芬

回答by minboost

Instead of redirecting, just show different views.

而不是重定向,只是显示不同的视图。

Here is some sample code:

下面是一些示例代码:

if ($this->input->server('REQUEST_METHOD') == 'POST')
{
    // handle form submission
    if (!is_null($data = $this->auth->create_user( $this->form_validation->set_value('username'), $this->form_validation->set_value('password') )))
    {
        // show success page
        $data['h1title'] = 'Successful Registration';
        $data['subtext'] = '<p>Test to go here</p>';

        // Load the message page
        $this->load->view('message',$data)
    }
    else
    {
        // show unsuccessful page
        $data['h1title'] = 'Unsuccessful Registration';
        $data['subtext'] = '<p>Test to go here</p>';

        // Load the message page
        $this->load->view('message',$data)
    }
}
else
{
    // show login page
    $data['h1title'] = 'Login';
    $data['subtext'] = '<p>Test to go here</p>';

    // Load the message page
    $this->load->view('login',$data)
}