CodeIgniter: Undefined property when calling the model - codeigniter

I use CodeIgniter 3 with HMVC.
Does anyone know why do I get the following error
Severity: Notice
Message: Undefined property: Login::$login_model
On the second line below. I clearly call the model, but for some odd reason it is not linked properly
$this->load->model('auth/login_model');
$response = $this->login_model->attempt($username, sha1($password));
Then the model is pretty basic :
<?php
class Login_model extends CI_Model
{
public function attempt($user, $pass)
{
...
Now if I use an object it works, but I have the same issue in many places, including the place where I have
$this->db->select("....
where is crashing as there is no "db". What is the new solution for CodeIgniter 3? I've seen older posts, but none seem to help me out
Thanks!

just try this code put in controller:
public function __construct() {
parent::__construct();
$this->load->model('Login_model'); // load model
}

Problem is resolved, the issues were caused by the fact that my controller extended CI_Controller instead of MX_Controller. So changing
class Login extends CI_Controller
to
class Login extends MX_Controller
resolved the issue.
It took me a while to figure it out by debugging the thirdparty/MX/Loader.php, but once I saw it was looking for MX_Controller type I did the change and it worked perfectly.
This issue is one in many related to migration from CI 2 to CI 3 and also using the HMVC from Wiredesignz. Another big one is uppercase of file names and uppercase on the calls, so strictly referring to this issue I had to uppercase the calls in my controller (changed "login" to "Login"):
$this->load->model('auth/Login_model');
$response = $this->Login_model->attempt($username, sha1($password));
I did the above change already, so this was no longer a blocker, still I wanted to put it here just in case someone hits the exact same issue
Thanks all for your help

Related

Illuminate\Database\Eloquent\RelationNotFoundException Call to undefined relationship [user] on model [App\Event]

I'm creating a view list for M-M relationship for Admin view. I want to display every user that has been registered for that particular events. And I'm pretty sure I called it wrong thats the reason why I getting this error. I'm not quite sure on how to fix it. I tried googling and I still don't find any solution yet. So how can I fix this?
The relation name is users not user. Please check using below code
use DB;
use App\Event;
use App\User;
public function show()
{
$events = Event::with('users')->get();
return view('admin.event.user')->with('events', $events);
}

How can I fix error: class not found using Laravel?

I'm trying to work on my model using Laravel.
Table name: refregions
Model: app/Refregion
Controller: in one of my function I have this code:
$regions = Refregion::all();
And I defined the model on controller
use app\Refregion;
Can someone tell me why class cannot be found?
Thank you.
Because you are using the wrong path of Model in your controller.
you have to use the following one instant of this one 'use app\Refregion;'.
use App\Refregion;
In your Controller you have to use like this below
use App\Refregion;
Or
use \App\Refregion;

How can I render a twig template in a custom controller in Silex?

I'm playing with Silex microframework to build a very simple app.
The Silex documentation briefly illustrates how to keep your code organised using controller as class and I've also found this useful article talking about the same practice:
https://igor.io/2012/11/09/scaling-silex.html
but still can't solve my problem
The issue:
in my app.php I'm using
$app->get('/{artist}', 'MyNamespace\\MyController::getAlbum');
This is working. MyController is a class correctly loaded through composer using psr-4.
At the moment the return method of getAlbum($artist) is return $player;
What I'd like to do instead, is returning a twig view from getAlbum, something like:
return $app['twig']->render('player.twig', $player);
To do so, what I've tried to do in my custom class/controller is:
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
[...]
public function getAlbum(Request $request, Application $app, $artist)
but this is generating the following error when I try to access the routed pages:
ReflectionException in ControllerResolver.php line 43:
Class MyNamespace\Request does not exist
Whic made me think that there's a namespace conflict between myNamespace and the Silex namespaces?!
What am I doing wrong?
Is this the right way to make $app visible in my custom controller in order to use return $app['twig']... ?
Thank you in advance!
EDIT
After several other tries still didn't get to the point (replies still welcome!) but I've found a workaround solution that could be useful to anyone will incur in a similar issue. Directly in my app.php I added this
$app->get('/{artist}', function (Silex\Application $app, $artist) use ($app)
{
$object = new MyNamespace\MyController();
$player = $object->getAlbum($artist);
return $app['twig']->render('player.twig',
array(
//passing my custom method return to twig
'player' => $player,));
});
then in my player.twig I added:
{{player | raw}}
And this basically means that I still need an anonymous function to get use of my custom method which is a working solution I'm not really happy with because:
I'm using 2 functions for 1 purpose.
The return value of getAlbum is dependent from the use of "raw" in twig.
SOLVED
The workflow described works fine. It was a distraction error: I've placed the namespace of my custom class after use Symfony\Component\HttpFoundation\Request;
namespace declaration in PHP needs always to be at the top of the file, Silex wasn't able to injects $app and $request for this reason.

Laravel 4: Multiple Action in a Single Contnroller Not Working?

I am new to Laravel and am just stuck upon this thing. I have set a controller which contains multiple actions for application. It turns out that if I put a single action in a single controller class, it works quite well. But when I use multiple action. It gives that damn error "Whoops, looks like something went wrong."
So, exactly what am I doing wrong? below is my controller class:
<? php
class ProgController extends BaseController
{
public function showHome()
{
return View::make('home');
}
public function showLogin()
{
return ('you are not authorized to login yet');
}
public function showTravel()
{
return View::make('travel');
}
}
and here is the route.php file content:
Route::get('/', 'ProgController#showHome');
Route::get('/login', 'ProgController#showLogin');
Route::get('/travel', 'ProgController#showTravel');
the files are in their respective default folders. I am worried has it something to do with composer.json file or what? what am I doing wrong?
Turn on debugging in your app/config/app.php file. Make sure you have a home.blade.php or home.php file in app/views (same applies for travel.blade.php or travel.php).
Unless we can see a stack trace that's the best we can give you for now. Definitely turn on debugging for a dev environment.
Use server log, it'll tell you the exact error when you have 500 like this "Whoops, looks like something went wrong.". Most likely you have one component which is not configured right or missing class, missing model, or anything which Laravel can not capture in application level.

CodeIgniter 2 and usage of $this->

I'm using CodeIgniter 2 and have installed Ion Auth and also the News tutorial that comes with CodeIgniter.
In the News Controller, the element for the page title is written like this...
$data['title'] = 'Page Title';
However, in the Ion Auth Controller, the element for the page title is written like this...
$this->data['title'] = 'Page Title';
They both seem to work equally well, so can anyone explain the difference(s)? Maybe Ion Auth was written for an older version of CodeIgniter? Is there any practical reason why I'd want to use one over the other? Please link to sources as needed.
I guess it's the author's preference. He likes to use a class property to store the view's data. It allows him to share it across methods. If you look at the author's other projects (Source 1, 2, 3), you can see two examples (source 1 & 2 goes together).
On a side note, for your project, this could allow you to extend the Auth controller with more view data.
class MY_Auth extends Auth {
function __construct()
{
parent::__construct();
}
function index()
{
$this->data['foo'] = 'bar';
parent::index();
}
}
That would allow you to use the $foo variable to your authentication view. (/auth/index in this case.)
In my own projects, I like to use a protected property for my view's data. It does give you much more freedom than a local variable. You don't need to pass the view's data as an argument all the time and you can easily extend your controllers afterward.
Hope this helps!
if you are going to use this $this->data it means you can access $this->data through out the class methods. On the other hand if you are using $data it is only available for the current scope or method and if you need data some where else then you will have to pass it as parameters to the other methods.
Adding $this on the data variable, makes it to be accessible through the class.
I believe the $data or $this->data is only used for "View". It will be passed from the "Controller" to the "View", so we can access that variable through the "View".
So, there will be no differences on the "View" side.

Resources