• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
load() method doesn't work

#1
Hello there,

since 1 week i started to get into the whole MVC World. And because my understanding of it equals null i was unsuccessful with CakePHP, Symfony or Laravel. Then i found the Codeigniter.

I was experimenting with Routing and AJAX and Controllers and now i want to make myself a template in the Views folder. I tried to include the so called header.php and footer.php in my home.php page but i doesn't work. After some research i found out that in Codeigniter you can't just do an include like in conventional php. 

After another research i found a way to do it but my Codeigniter seems to not recognize the method i am using although for everyone else works without for them to do anything.
My Code looks like this:

PHP Code:
<?php namespace App\Controllers;


class 
Home extends BaseController
{

    public function 
index()
    {
        
$pageData = [

            
'header' => $this->load->view('header.php'NULLTRUE),
            
'footer' => $this->load->view('footer.php'NULLTRUE),
            
'tabTitle' => 'Online Shop MTFKS!'
        
];

        return 
view('home.php'$pageData);
    }

    
//--------------------------------------------------------------------



This call $this->load seems not to be existent. My PHPStorm throws me an error everytime i click on it.


[Image: jSNcdf0.png]

I am stuck and i don't know how can i go further.

I have to mention that when i installed the Codeigniter Framework i just downloaded it and extracted it in my project folder as mentioned in the Docs. Then i followed the docs to finish setting it up. But somehow i think something is missing. And whenever i try to install it composer it just doesn't run, my powershell is stuck on an empty line.

When i run this code my website just gives me the standard CI Whoops! Error.

Thank you !
Reply

#2
You're mixing versions 3 and 4.
PHP Code:
$this->load->view('your-view'$data); 

is CodeIgniter 3. In CI4, use
PHP Code:
echo view('your-view'$data); 
Reply

#3
Because you are doing it wrong, there is no load view anymore.

PHP Code:
<?php namespace App\Controllers;


class 
Home extends BaseController
{

    public function index()
    {
        $pageData = [
            'tabTitle' => 'Online Shop MTFKS!'
        ];

    echo 
view('header');
        echo view('home'$pageData);
        echo view('footer'),
    }

    //--------------------------------------------------------------------



That's how you do it now.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
Thank you very much ! I was looking in stackoverflow the whole time but i forgot that i was starting with CI 4 and not CI 3 and that the posts are outdated.

Now i did it this way to save some time.

Controller/Home.php

PHP Code:
<?php namespace App\Controllers;


class 
Home extends BaseController
{

    public function 
index()
    {
        
$headerData = [
            
'tabTitle' => 'Online Shop',
            
'tabHome' => 'active'
        
];

        echo 
$this->viewTemplate('home.php'$headerData);
    }

    
//--------------------------------------------------------------------



Controller/BaseController

PHP Code:
public function viewTemplate($viewName$headerData = [], $footerData = [], $viewData = [])
    {
        echo 
view('header.php'$headerData);
        echo 
view($viewName$viewData);
        echo 
view('footer.php'$footerData);
    } 

Now it works great.
Reply

#5
You might want to look into new View Layouts also, and see if they are something you're interested in.
Support Development  • Practical CodeIgniter 3  •
Myth:AuthVulcan - CLI Tools for CI4
Reply

#6
And the view parser :-)
https://codeigniter4.github.io/CodeIgnit...arser.html
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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