Laravel - Model Method from Child Controller - php

I have a base controller which injects a User model in it's constructor:
class BaseController extends Controller {
public $user;
public function __construct(User $user) {
$this->user = $user;
View::share('user', $this->user);
}
AuthController extends the BaseController
class AuthController extends BaseController {
public function __construct(LDAP $ldap, User $user) {
parent::__construct($user);
$this->ldap = $ldap;
$this->user->setUsername('Username'); //This is not being called
}
How can I access the injected model from the parent controller and call methods on it?
If I use $this->user->setUsername('Username'); from the BaseController the method is called correctly, but not from the child controller.

Related

Dynamic type-hint in Laravel for Form Request

I've created a Base Controller and I want to dynamically type-hint the store method to use the proper Form Request class. How can I do that?
Here's my base controller (simplified):
class BaseController extends Controller
{
protected $baseClass;
protected $baseResourceClass;
protected $baseStoreRequestClass;
public function index()
{
$items = $baseClass::paginate(10);
return $baseResourceClass::collection($items);
}
// the $baseStoreRequestClass doesn't work, and that's what I'm trying to figure it out
public function store(**$baseStoreRequestClass** $request)
{
$validatedFields = $request->validated();
$newItem = $baseClass::create($validatedFields);
return new $baseResourceClass($newItem);
}
}
Then, from the controller that will extend, I would have just to declare the 3 variables. Example:
class UserController extends BaseController
{
protected $baseClass = '\App\User';
protected $baseResourceClass = '\App\Http\Resources\UserResource';
protected $baseStoreRequestClass = '\App\Http\Requests\StoreUser';
}
class ProductController extends BaseController
{
protected $baseClass = '\App\Product';
protected $baseResourceClass = '\App\Http\Resources\roductResource';
protected $baseStoreRequestClass = '\App\Http\Requests\StoreProduct';
}
How could I make the $baseStoreRequestClass works?
You can't specify a dynamic type as a function parameter. It's just not valid PHP syntax. Here's what I suggest. Your base class would be the boilerplate:
class BaseController extends Controller
{
protected $baseClass;
protected $baseResourceClass;
public function index()
{
$items = $baseClass::paginate(10);
return $baseResourceClass::collection($items);
}
public function store(FormRequest $request) // Or other base request object you might create
{
$validatedFields = $request->validated();
$newItem = $baseClass::create($validatedFields);
return new $baseResourceClass($newItem);
}
}
Then each subclassed controller would need an explicit request type:
class UserController extends BaseController
{
protected $baseClass = '\App\User';
protected $baseResourceClass = '\App\Http\Resources\UserResource';
public function store(StoreUser $request) {
return parent::store($request);
}
}

Class 'App\Model\Users' not found in Codeigniter4

I am working with the latest version of codeigniter framework. Something is wrong with my code, it gives me an error like:
Class 'App\Model\Users' not found
Controller
Filename: Auth.php
<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use App\Model\Users as CodeIgniterUsers;
class Auth extends ResourceController
{
public function login()
{
$model = new CodeIgniterUsers();
var_dump($model);
}
public function register()
{ }
}
Model
File name: Users.php
<?php
namespace App\Model;
use CodeIgniter\Model;
class Users extends Model
{
protected $db;
protected $table = 'user';
protected $returnType = 'array';
protected $allowedFields = ['name', 'email', 'password'];
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
}
If you haven't change your directory names in app, you need to change namespaces from App\Model\Users (without "s" at the end) to App\Model\User.
Namespaces should follow directory structure, unless you change (or extends) CI4's core classes or at least app/Config/Autoload.php
Extends your model with CI_Model for it will be recognized .
class Auth_model extends CI_Model {
//you can always put function construct
public function __construct (){
parent::__construct ();
}
}
In controller :
class User extends CI_Controller {
public function __construct () {
parent:: __construct();
//you can load here the model that you will just often so will load it everytime to use it in a function
$this->load->auth_model('model-name(exam- public function test_uer()');
}
}

Laravel view share doesnt work in modularing system

simply i can use modular system in laravel, but i cant use view share in this solution,
app/Http/Controllers/Controller.php
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct()
{
view()->share('name', 'MY NAME');
}
}
app/Modules/NewCurrency/Controllers/IndexController.php
class IndexController extends Controller
{
public function __construct()
{
parent::__construct();
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('layouts.admin.new_user.index');
}
}
resources/views/layouts/admin.new_user.index.blade.php
{{ $name }}
I get this Error:
Undefined variable: name
Did you check this
View::share()
Because the method that you wrote is used by other way. You should place calls to share within a service provider's boot method.
<?php
namespace App\Providers;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
view()->share('key', 'value');
}
public function register()
{
//
}
}

Multiple construct functions

I'm trying to initizalize a couple of helper classes into my laravel controller only problem is I have 3 things to initialize but only one constructor message for exmaple:
<?php
use UG\Validation\Forms\Login as LoginForm;
class SessionsController extends \BaseController {
protected $loginForm;
public function __construct(LoginForm $loginForm)
{
$this->loginForm = $loginForm;
}
That is to help validate the forms but now I also want to add a repository to help me with keeping eloquent out of my controller
<?php
use UG\Repositories\User as User;
class SessionsController extends \BaseController {
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
now the problem Im facing is that both these methods need to go in my controller but I only have one _construct method. So how would I go about this
Just put both classes in the constructor:
class SessionsController extends \BaseController {
protected $user;
protected $loginForm;
public function __construct(User $user, LoginForm $loginForm)
{
$this->user = $user;
$this->loginForm = $loginForm;
}

Calling a method from AppModel class from its child or Controller

Hello this is my AppModel.php Class
<?php
App::uses('Model', 'Model');
class AppModel extends Model{
static public function message()
{
return 'this is a message';
}
}
and I have my model User.php
<?
class User extends AppModel {
}
and my controller UsersController.php
class UsersController extends AppController
{
public function index()
{
$this->layout ='main';
}
}
My question is, how can I call method message() from its AppModel Class in UsersController or at least in my model Users?
You can call it like you do with any static method
AppModel::message();
Though, I suggest not using it as static. In your controllers and definitely in your models you will have already an instance of a model that extends the AppModel. So if you change
/*static*/ public function message()
{
return 'this is a message';
}
then you can call it in controllers like
$this->User->message();
and in the user model with
$this->message();
And while we're on it, change it to protected so only it's children can use the function.

Categories