Trying to get property of non-object prestashop - prestashop-1.7

I am trying to assign a variable inside a controller. I have written this statement in the __construct() function of the controller but I am getting the error trying to get property of non-object. If I remove the statement everything works fine. The statement is okay since that is used to assign variables according to the prestashop documentation but I think it doesn't have to be written in the __construct() function when working with controllers. Does anyone know where should I write the assign statement?
Statement:
$this->context->smarty->assign('message', 'hello');

You have to define the __constructor like this:
public function __construct()
{
$this->context = Context::getContext();
$this->bootstrap = true;
parent::__construct();
}
then, define initContent function:
public function initContent()
{
$this->context->smarty->assign('message', 'hello'); --> Define here
parent::initContent();
}

Related

Pass variables to multiple view in laravel

I want to pass a variable to multiple view bu when i use share method in View. Its says the share method isn't find on View.
How you say i use it and i try the composer either but no matter how i try it can't work could you give me simple example of this action
My controller categorycontroller.php
public function site(){
$data = array();
$data['subcategories'] = SubCategory::all();
$data['categories'] = Category::all();
return view('template.sitemap',compact("data"));
}
My route web.php
Route::get('/404.html', function () {
return view('template/404');
})->name('404.html');
Route::get('404.html','CategoryController#site')->name('404');
Route::get('/sitemap.html', function () {
return view('template/sitemap');
})->name('sitemap.html');
Route::get('sitemap.html','CategoryController#site')->name('sitemap');
what do you suggest?
You can make a variable accessible in multiple views using one of these methods for example:
AppServiceProvider ( reference: https://laravel.com/docs/5.6/providers ) with ViewComposer ( reference: https://laravel.com/docs/master/views#view-composers )
You'll need to add to your ServiceProvider boot() method something similar to this:
public function boot()
{
View::share('variable_name', 'some_value_here');
}
Or inside a controller:
public function __construct() {
$something = 'just a test';
View::share('something', $something);
}

Pass variable from one method to another in same controller Laravel

I need to pass select option value from store method to show
i need to pass the $typeg value to show method
public function store(Request $request ) {
$typeg = $request->input('type');
}
public function show($id) {
dd($this->store($typeg));
}
i get
Undefined variable:
or
Too few arguments to function app\Http\Controllers\appController::show(), 1 passed and exactly 2 expected
Try this
on the first function you have some variable witch you want to pass it to another function\method
Than you need to use $this and the name of the other method you'd like to pass the var too something like this.
public function oneFunction(){
$variable = "this is pretty basic stuff in any language";
$this->anotherFunction($variable)
}
public function anotherFunction($variable){
dd($variable);
}
Store your data on session (or somewhere else like cookie, cache, database). So you can reach the data later.
class SomeController extends Controller {
public function store(Request $request ) {
session(["typeg"=>$request->input('type')])
}
public function show($id) {
dd(session("typeg"));
}

recall the construct in codigniter

How to recall the construct as it contains all the required data for the page?
class Abc extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('xyz_m');
$this->data['info'] = $this->xyz_m->get(); //get data
}
public function 123()
{
/*view page code*/
}
public function 456()
{
/*insert code here*/
$this->123(); // redirect, need to load 123() with updated data from construct.
}
}
So, how do you make the __construct initiate again so you get a new updated results from database?
You should name your methods with letter first i.e. there is convention for method names uses descriptive words getProducts() or get_books or you will get PHP error for using numbers as method names. So in your case method names should be like a123() or b_456().
Second thing, regarding your need in question, since you assign data from DB using model to array $this->data, you would use it like:
class Abc extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('xyz_m');
$this->data['info'] = $this->xyz_m->get(); //get data
}
public function a123()
{
$this->load->view('a123_view', $this->data);//loading file APPPATH . 'a123_view.php' and passing created array to it
}
public function b_456()
{
/*insert code here*/
$this->a123(); // redirect, need to load 123() with updated data from construct.
}
}
In your APPPATH . 'a123_view.php':
<?php var_dump($info);//here you would call key of array you passed from controller as variable ?>
Check basics in CodeIgniter documentations. All this is described in General Topics section.

CodeIgniter - Call to a member function ... on a non-object from Model [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 9 years ago.
I'm using CI 2.1.3 and I'm having issues with loading models and referring to the CodeIgniter "super object".
For example:
I'm trying to log in using my Login controller:
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Users_model');
}
public function validate_credentials() {
$this->load->library('form_validation');
// Some form validation here...
$query = $this->Users_model->validate();
if($query) { // if the user's credentials validated...
// something
} else {
// something
}
}
And when it gets to my Users_model:
class Users_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function validate()
{
$this->db->where('active', 1);
$this->db->where('email', $this->input->post('email'));
$this->db->where('password', md5($this->input->post('password')));
$query = $this->db->get('user');
if($query->num_rows == 1)
{
return $query;
}
}
}
I get an error "Fatal error: Call to a member function where() on a non-object in users_model.php on line XX" referring to the first line of validate() function.
I can get it working by using double colon (::) operator in Login controller like Users_model::validate() but I think I shouldn't need that.
I can also get it working by writing something like:
$ci = get_instance();
$ci->db->where...
at the start of the Users_model->validate() function, but I think I shouldn't need to do that either.
The database class is being loaded in autoload.php.
So the problem is that $this in my models refers to the model class itself rather than the "super object" it's supposed to. I have no moves left and I'm guessing it's about something very simple but I just can't see it. Please help me.
Please try by loading CI library like this
$this->load->library('database');
in __construct() function before loading model:
or load this library from autoload file like:
$autoload['libraries'] = array('database','form_validation', 'session');

CI Bonfire: Cannot call methods on Model

I am new to BF. I am learning BF by following the tutorial here, but I cant figure out why it keep prompt me error message as below:
Fatal error: Call to a member function where() on a non-object in ...
Here is the code:
class Content extends Admin_Controller {
public function __construct(){
parent::__construct();
Template::set('toolbar_title', 'Manage Your Blog');
}
public function index(){
$posts = $this->post_model->where('deleted', 0)->find_all();
Template::set('posts', $posts);
Template::render();
}
}
Can someone guide me on this? Thanks
bborisovs is right, you need to load the model in the constructor, before you can use the obgject:
$this->load->model('post_model', null, true);
Also make sure your model exists.

Resources