douxu2081 2014-08-16 17:07
浏览 23
已采纳

CodeIgniter控制器/视图困境

I have created a simple register/login system and it (I think) works well so far.

Now I want to implement some of it's functions to my site.

controllers/main.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller
{
    public function index()
    {
        $this->home();
    }

    public function home()
    {
        $this->load->view('home.php');
    }
}

views/home.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Home</title>
</head>
<body>

<div id="container">
    <?php if ($this->session->userdata('is_logged_in')): ?>

        logged in as: <?php echo $this->session->userdata('username'); ?></br>
        <a href="<?php echo base_url().'login/logout'; ?>">Logout</a>

    <?php else: ?>

        not logged in</br></br>

        <?php echo form_open('login'); ?>
        <?php echo form_error('username'); ?>
        <input type="text" name="username" placeholder="Login" />
        </br>
        <?php echo form_error('password'); ?>
        <input type="password" name="password" placeholder="Password" />
        </br></br>
        <div><input type="submit" value="Sign in" /></div>
        <?php echo form_error('login_validation'); ?>
        </form>

    <?php endif ?>
</div>

</body>
</html>

Is this the right way of doing things in CI?

  • 写回答

1条回答 默认 最新

  • duanou2016 2014-08-16 17:34
    关注

    For controller part instead of using the home(), you can directly load the view in index() because internally you are navigating from index to home() without any specific reason.

    Also the loading view doesn't require ".php" extension in your file and you can do like this:

    $this->load->view('home');
    

    Where home.php is the file located inside application/views/

    For showing the error messages, you can use another optimal way by defining the validation rules(in array fromat) inside config/form_validation.php and just require below code to show all messages instead one by one

    <?php echo validation_errors()?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问Ubuntu要怎么安装chrome呀?
  • ¥15 视频编码 十六进制问题
  • ¥15 Xsheii7我安装这个文件的时候跳出来另一个文件已锁定文件的无一部分进程无法访问。这个该怎么解决
  • ¥15 unity terrain打包后地形错位,跟建筑不在同一个位置,怎么办
  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊