dpzff20644 2012-09-27 23:51
浏览 30
已采纳

如何在不同的视图上重用此代码? 适当的方法

I am somewhat confused on the proper way to perform the following. Is this a class a function or an object? (Not sure) So here goes (please be kind) I'm learning codeigniter/php simultaneously.

    $is_live = $this->session->userdata('email');
    $is_live = ucwords($is_live);
            if (!$is_live == null)
                   echo 'Hello, '.$is_live.
                   ' <b>(Not you? '.'<a href="' .base_url().
                   'main/logout">Log Out</a>) </b>'; 

I wrote this code, which checks if a session is set and if true echo the details. I currently have this code on my view/main However I want to have this code displayed on all pages.

What is the proper way to do this?

  1. Do i create a view/header which is auto loaded on each page and insert this code there?
  2. Do I create a class/public function in a helper file and load it where needed?
  3. Do I create a class/public function in a library file and load it where needed?
  4. Do I create a public function in a controller?

-EXTRA- My assumption is option 1 but I'm curious what if I want to display this information in another location on a particular page? I don't want to have to copy/paste the code block because that is sloppy.

How would I set it up so that I can just call it where i need it? e.g. $this->load->helper('check_for_sess');

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

class isLive
{   
    public function is_live() {

        $is_live = $this->session->userdata('email');
        $is_live = ucwords($is_live);
             if (!$is_live == null)
               echo 'Hello, '.$is_live.
               ' <b>(Not you? '.'<a href="' .base_url().'main/logout">Log Out</a>) </b>'; 

    }
}

On the view I tried this:

<?php 
    $isLive = new isLive;
    $isLive->is_live(); 
?>

This however doesn't work and causes a ' function userdata() on a non-object error '

Can someone please explain the proper way to achieve my desired solution and the correct syntax. Thanks in advance!

-UPDATE-- Tried to create a library - still getting error.

#Created a library 
class CheckSess 
{   
    function IsLive() 
    {   
    $is_live = $this->session->userdata('email');
    $is_live = ucwords($is_live);

            if (!$is_live == null) 
            {
               $check_msg = 'Hello, '.$is_live.
               ' <b>(Not you? '.'<a href="' .base_url().'main/logout">Log Out</a>) </b>';
            }

    return $check_msg;
    }

}


#In the View
<?php echo $this->CheckSess->IsLive();  ?>

#in the controller
$this->load->library('CheckSess');

--UPDATE--

class CheckSess {

    function IsLive() 
    {   
    $CI =& get_instance();
    $is_live = $CI->session->userdata('email');
    $is_live = ucwords($is_live);

            if (!$is_live == null) 
            {
               return 'Hello, '.$is_live.
               ' <b>(Not you? '.'<a href="' .base_url().'main/logout">Log Out</a>) </b>';
            }

    }

}

Set this^ is the libraries folder

the view shows <?php echo $this->CheckSess->IsLive(); ?>

and the controller shows $this->load->library('CheckSess');

please advise. Thanks again!

ERROR---

( ! ) SCREAM: Error suppression ignored for ( ! ) Fatal error: Call to a member function IsLive() on a non-object in C:\wampserver\www\test-app\application\views\homepage.php on line 56 Call Stack

Time Memory Function Location

1 0.0005 151432 {main}( ) ..\index.php:0 2 0.0014 187792 require_once( 'C:\wampserver\www\test-app\system\core\CodeIgniter.php' ) ..\index.php:202 3 0.0277 1537000 call_user_func_array ( ) ..\CodeIgniter.php:359 4 0.0277 1537048 Main->index( ) ..\CodeIgniter.php:359 5 0.0283 1540200 CI_Loader->view( ) ..\main.php:8 6 0.0283 1540640 CI_Loader->_ci_load( ) ..\Loader.php:419 7 0.0287 1566584 include( 'C:\wampserver\www\test-app\application\views\homepage.php' ) ..\Loader.php:833

  • 写回答

2条回答 默认 最新

  • douyan1244 2012-09-28 00:19
    关注

    The best ways to write such reusable in CodeIgniter are Models and Libraries.

    With libraries being better because the code and logic you use isn't actually what models in MVC are, but with CodeIgniter and especially for a beginner it's pretty much ok to write this in models if you are not planning to distribute or share this code in any way.

    I would suggest reading more on what goes where in CodeIgniter, you can find plenty information here, on SO or on official CI forums, but as a quick reference, I will write a list of what goes where.

    Views

    Static, dumb data output, formatting, iteration(for displaying tables, lists, etc.).

    The only things you want to use here are some of the basic PHP functions, CodeIgniter helpers, for, foreach, while loops and conditionals (if, case)

    Controllers

    Form data capture and validation, catching data from models, sending data to views, redirects.

    Models

    Querying, reading, writing and updating data from databases, remote sources (APIs), files.

    Helpers

    Often used functions that are expected to be used in views - formatting HTML, parsing and displaying trees as lists <ul><li></li></ul>, shorthand functions for other functions like

    function mydate() {
        return date("d.m.Y");
    }
    

    Stuff that does not use any of the CodeIgniter libraries/models (Technically it can use, but should be avoided in most cases).

    Libraries

    Custom classes and objects, data processing, API interfaces, complex of the aforementioned



    If you want to display some view file with data, I would put it in a library. You can put it in to a model. In my projects when I use some parts, I have a library/model (difference in our case is minimal), lets call it pmodel.

    class Pmodel extends CI_Model() {
        function ShowHeader() {
            $data = array(
                'user' => $this->usermodel->GetCurrentUser()
            }
            $this->load->view('header', $data);
        }
    
        function ShowMyPart() {
            $is_live = $this->session->userdata('email');
            $is_live = ucwords($is_live);
            if (!$is_live == null)
                   echo 'Hello, '.$is_live.
                   ' <b>(Not you? '.'<a href="' .base_url().
                   'main/logout">Log Out</a>) </b>'; 
    
        }
    }
    

    Now anywhere in the view files you can call $this->pmodel->ShowMyPart() if you have loaded it before (models of this type should be autoloaded anyway).

    There is one little change I would do as it's considered as good practice - never echo anything from anywhere but the view files, return values instead.

    Change the echo keyword to return and in your view file simply write echo $this->pmodel->ShowMyPart().

    Why should you do that? Because, if you would like to call any method that echoes something from the controller right between loading header and footer(eg.), CodeIgniter will echo this before the header as CodeIgniter does not echo your loaded views right away, it saves them to output storage and sends the finalized output right before destructing himself. More reading can be found here.

    But you can do even better - use a view file to output any data you want, just create a helper and load it from your model/library as you do in your controllers.

    Hope that helps!


    EDIT

    For your edited question, the reason why you are getting errors with your library is that your library doesn't know that an instance of CodeIgniter even exists as you are not extending your library from it, as you do with your models and controllers. You don't use $this to refer to CodeIgniter classes in your libraries, as $this is a reference to your library itself, which is a separate class. To use CodeIgniter classes you should get a CodeIgniter instance:

    $CI =& get_instance();
    

    And then you can use $CI instead of $this to call CodeIgniter classes by changing the line:

    $is_live = $this->session->userdata('email');
    

    to

    $is_live = $CI->session->userdata('email');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?