• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Practical CodeIgniter 3 - A new book coming soon

#34
The simplest solution is to put the code in that chapter into your MY_Controller file. The My_Controller example file that you should have received with the book has the functions already in there. Then, all of your controllers would extend from that. At this point, all of the functions are available to your controllers. Then, the $this->render() method would display the current view based on your controller and message name. We'll walk through a quick example assuming a user profile page.

First, create your main "layout" file. This is the template that is used for all of the pages that your content gets inserted into. Make sure the echo out a view_content variable.

Code:
<!doctype html>
<html>
<head>
    <title>Some Great Site</title>
    <link rel="stylesheet" href="/assets/css/app.css">
</head>
<body>
    <!-- Header stuff goes here -->

    <div class="container">
        <?= $view_content ?>
    </div>

    <!-- Footer stuff goes here -->
</body>
</html>

Then, in your controller, you might do something as simple as:

Code:
class Users extends MY_Controller {
    public function index() {
        $data = [
            'user' => $this->auth->current_user();
        ];

        $this->render($data);
    }
}

This will look for a view file at application/views/users/index.php. You would fill that view file with any content that you needed to, and it would be inserted into the layout file at the place where the $view_content is output.

There's more details to it, of course, but is that enough for it all to make sense? Sounds like I need to clarify that part of the book a little more with an example or two.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


Messages In This Thread
RE: Practical CodeIgniter 3 - A new book coming soon - by kilishan - 02-15-2016, 08:43 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.