codeigniter 4 Undefined property: App\Controllers\Test::$load - codeigniter-4

I'm switching to Condeigniter 4.
Trying to do some basic stuff.
I can't load the model into controller function.
Controller Test.php:
<?php
namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$this->load->model('user_model');
}
}
Model User_model.php
<?
namespace App\Models;
use CodeIgniter\Model;
class User_model extends Model
{
public function get_user_info($user_id)
{
return 1;
}
}
I'm getting error: Undefined property: App\Controllers\Test::$load
What am I doing wrong?

Codeigniter 4 and 3 have different way to load model. In codeigniter 4 you can use
use CodeIgniter\Model\User_model;
$modelUser = new \App\Models\User_model();
or
$userModel = model(User_model::class);
or you can see in Codeignter 4 User Guide, Accessing Models

Model loading approach has been changed to Codeigniter 4. The way of loading the models that you use in your code has been replaced. Check out the library Models and information about loading the model can be found here Accessing Models.
Controller - test code
<?php namespace App\Controllers;
class Test extends BaseController
{
public function index()
{
$TestModel = new TestModel;
$this->data['test'] = $TestModel->test(2);
return view('App\Views\test',$this->data);
}
}
Model - Test code
<?php namespace App\Models;
use CodeIgniter\Model;
class TestModel extends Model
{
public function test($var = 1)
{
return $var;
}
}
The above sample code allowed us to reference the $test variable in the view, which will display the value 2.

Related

Express + Typescript + Objection pass model to super class using generic type not working

class BaseService<Models> {
public m: Models;
constructor(model: Models) {
this.m = model;
}
}
class MyService extends BaseService<UserModel>{
constructor() {
super(UserModel); //It is require new keyword
}
}
We are using knex.js with Objection.js models.
I'm able to run queries in MyService class.
e.g.
const user = await UserModel.query().findById(1);
But instead i want a base service that to set Model using generic type.
What i need from child class id.
super(UserModel); //It is not working
If i pass UserModel using new keyword then error removed but queries not working
super(new UserModel())
Typescript Playground Link
You tried to pass class to super class, but it expected instance of that class
class BaseService<Models> {
public m: Models;
constructor(model: Models) {
this.m = model;
}
}
class UserModel {}
class MyService extends BaseService<typeof UserModel>{
constructor() {
super(UserModel); //It is require new keyword
}
}
new MyService();

Laravel7 Call to undefined method Illuminate\Database\MySqlConnection::find()

in my Laravel 7 app when i go to a page i am getting this
Call to undefined method Illuminate\Database\MySqlConnection::find()
In my app/Http/Controllers/clientController.php I have
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Client;
class ClientController extends Controller {
public function viewclient(Request $id)
{
$client = DB::find($id);
return view('viewclient', compact('client', 'id'));
}
}
In my app/Client.php I have
namespace App;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
protected $table = 'clients';
}
Not sure what I am missing.
You should mention the table name when use query builder :
$client = DB::find($id); // no table here
Change to
$client = DB::table('clients')->find($id); // here 'clients' is your table name
See Official Documentation

Codeigniter 4 - call method from another controller

How to call "functionA" from "ClassA" in "functionB" inside "ClassB" ?
class Base extends BaseController
{
public function header()
{
echo view('common/header');
echo view('common/header-nav');
}
}
class Example extends BaseController
{
public function myfunction()
// how to call function header from base class
return view('internet/swiatlowod');
}
}
Well there are many ways to do this...
One such way, might be like...
Assume that example.php is the required Frontend so we will need a route to it.
In app\Config\Routes.php we need the entry
$routes->get('/example', 'Example::index');
This lets us use the URL your-site dot com/example
Now we need to decide how we want to use the functions in Base inside Example. So we could do the following...
<?php namespace App\Controllers;
class Example extends BaseController {
protected $base;
/**
* This is the main entry point for this example.
*/
public function index() {
$this->base = new Base(); // Create an instance
$this->myfunction();
}
public function myfunction() {
echo $this->base->header(); // Output from header
echo view('internet/swiatlowod'); // Output from local view
}
}
When and where you use new Base() is up to you, but you need to use before you need it (obviously).
You could do it in a constructor, you could do it in a parent class and extend it so it is common to a group of controllers.
It's up to you.

AdonisJs: Using Hashids

Had already someone used Hashids in AdonisJs?
Being more specific, in a Model, to return a property hashid in an object
I'm working on a migration from Laravel to Adonis.
In Laravel, it's possible just with a couple of code lines in each Model, like this:
use Hashids;
class Menu extends Model
{
use \OwenIt\Auditing\Auditable;
protected $appends = ['hashid'];
public function getHashidAttribute()
{
return Hashids::encode($this->attributes['id']);
}
}
I installed this NPM package: https://www.npmjs.com/package/adonis-hashids, and I tried to figure out how to use like the Laravel way
I'd used Computed Properties (https://adonisjs.com/docs/4.1/database-getters-setters#_computed_properties)
class Menu extends Model {
static get computed () {
return ['hashids']
}
getHashids({ id }) {
return Hashids.encode(id)
}
}

Symfony2, How to secure all controllers in one place using custom function?

I have defined my controller but I would like to secure ALL of them like below :
// In my Controller Class
public function chooseDateAction()
{
if($this->get('MY.roles_features')
->isGranted($this->container->get('request')->get('_route')))
{
// Do something
}
else
{
throw new AccessDeniedException();
}
return array( );
}
I had to design my own 'isGranted' function because roles are dynamic. BTW the function is working properly !
So my question is do I have to repeat the isGranted function in all of my Controllers or I can put it somewhere to reduce the code redundancy.
I know I have to place isGranted in some top level layer of my security, But the question is how and where ?
Try writing a base controller, which will check upon construction, if isGranted method passes, else throws exception. e.g.:
<?php
namespace Acme\DolanBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class BaseController extends Controller
{
public function __construct()
{
if(!$this->get('MY.roles_features')
->isGranted($this->container->get('request')->get('_route')))
{
throw new AccessDeniedException('Gooby pls');
}
}
}
Then just extend the BaseController in your other controllers.

Resources