Send data to views
Send data from Controllers to Views.
Commonly you will like to use your Controllers to handle many things like:
Logic of your app.
Database operations.
Send emails.
Return Views with or without data.
In this example we are going to touch the return data feature.
Go to:
You will see the next code:
Helper: view() returns a view file.
Params: you could send one variable or data by pass as second param the name of your variable in the view as string, this is how you could call the variable in your view file.
And next as third param you could send the variable value that you want to the variable in the view.
In the next example you will "echos" a variable user in View file like this:
Go to view:
And write:
Alternativly if you want to send more than one value to your view, in your controller:
Other way more elegant to send data as array could be using a native function compact() :
For more details about compact you could go to : PHP.net doc.
Now you could print your data in the view like this:
Or if its an array like data from a database select:
Last updated
Was this helpful?