0

As mention in the title I am trying to send the Session variables in the View Section after I login, The Session contains let's say name and email.

So what I have tried in the Profile Controller Section is below:

class ProfileController extends Controller{
public function index()
{ 
    $session = session();

    $userDetails = ['name' => $session->get('name'),
                     'email' => $session->get('email')];
    print_r($userDetails);
    echo view('profile', $userDetails); 
}

}

although I am able to print the $userDetails value in the controller Section, the same I am trying in VIEW is giving me nothing.

here's my profile.php code in view.

<h3>Welcome to the Profile Section : <?php echo $userDetails['name']; ?></h3>
<p>Your email id : <?php echo $userDetails['email'] ; ?></p>

The output I am getting is :

Result Image

Please correct me if I am missing something as I am new to the PHP frameworks.

2 Answers 2

2

In your View you would just have the array named indexes as they are converted to variables.

So $userDetails['name'] becomes $name and $userDetails['email'] becomes $email

So in your case. Instead of

<h3>Welcome to the Profile Section : <?php echo $userDetails['name']; ?></h3>
<p>Your email id : <?php echo $userDetails['email'] ; ?></p>

You would have.

<h3>Welcome to the Profile Section : <?php echo $name; ?></h3>
<p>Your email id : <?php echo $email ; ?></p>

And you can replace <?php echo with the shorter version <?=

<h3>Welcome to the Profile Section : <?= $name; ?></h3>
<p>Your email id : <?= $email ; ?></p>

I would strongly suggest to read the CodeIgniter Userguide as it covers this and everything else the framework does. So it's good to get familiar with it.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use compact function support by laravel to make array from your variable, and return a View you want

 return view('profile', compact('name','mail'));

At view you can call it by {{$name}} like this:

 <h3>Welcome to the Profile Section : {{$name}}></h3>

3 Comments

I appreciate your effort. but the question I asked was for codeigniter framework, I don't know that the above code will work for it though.
The answer offers quite a framework agnostic way of doing things. Also, mind the way you render your view — the proper way might differ from what you do (e.g. return view or $this->load->view instead of echo view)
as I am loading multiple views in the controller, let's say header, body and footer. the return view is returning only header view. and $this->load->view() is giving error in CodeIgniter4. That's why I have used echo view() @EduardSukharev

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.