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.
Related
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()');
}
}
I would like to know if this is possible in Object Oriented Programming in php. I have a controller
class BaseController extends Controller{
/**
* #Route("/sample", name="sample")
*/
public function postSampleAction(Request $request){
}
}
and I have a file called ProductEvent and PriceEvent
class ProductEvent extends BaseController{
public function checkEvent(){
echo "product event";
}
}
class PriceEvent extends BaseController{
public function checkEvent(){
echo "price event";
}
}
as you can see I extend the BaseController. What I want to happen is that I need to put the checkEvent() to the BaseController in postSampleActioin()
class BaseController extends Controller{
/**
* #Route("/sample", name="sample")
*/
public function postSampleAction(Request $request){
$this->checkEvent();
}
}
I don't know if this is a proper way. I want to test if that will echo the checkEvent() function.
Sorry my mistake. I forgot to add what framework do I used. I used symfony for this.
That means you want to make BaseController an incomplete, abstract class which requires to be inherited and the checkEvent method to be implemented there:
abstract class BaseController extends Controller {
public function postSampleAction(Request $request) {
$this->checkEvent();
}
abstract public function checkEvent();
}
You now cannot instantiate BaseController by itself, and any inheriting non-abstract class needs to implement checkEvent; that gives you the required type safety that allows you to depend on checkEvent in postSampleAction.
Is it OK to use properties/methods from parent classes in trait methods?
This code works, but is it good practice?
class Child extends Base{
use ExampleTrait;
public function __construct(){
parent::__construct();
}
public function someMethod(){
traitMethod();
}
}
trait ExampleTrait{
protected function traitMethod(){
// Uses $this->model from Base class
$this->model->doSomething();
}
}
I don't think it's good practice.
Instead you could have a method to fetch your model object, and have that method as an abstract signature in you trait:
trait ExampleTrait {
abstract protected function _getModel();
protected function traitMethod() {
$this->_getModel()->doSomething();
}
}
class Base {
protected $_model;
protected function _getModel() {
return $this->_model;
}
}
class Child extends Base {
use ExampleTrait;
public function someMethod() {
$this->traitMethod();
}
}
Or pass your model as a parameter to your trait method:
trait ExampleTrait {
protected function traitMethod($model) {
$model->doSomething();
}
}
class Base {
protected $_model;
}
class Child extends Base {
use ExampleTrait;
public function someMethod() {
$this->traitMethod($this->_model);
}
}
Both of these approaches let you utilize your IDE's type hinting.
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.
class ControllerBase
{
}
class postSmsController extends ControllerBase
{
}
class SmsSentController extends ControllerBase
{
}
How to extends CotrollerBase along with other controller in phalcon, I want to use the functions of postSmsController in SmsSentController, also want to use function of ControllerBase in both Controller classes. what should i do
You can extend one from another
// Base controller
class ControllerBase
{
public function one()
{
}
}
// Extending. Offers one() and two()
class postSmsController extends ControllerBase
{
public function two()
{
}
}
// Extending. Offers one() and two() and three()
class SmsSentController extends postSmsController
{
public function three()
{
}
}
// Offers only one()
class otherController extends ControllerBase
{
}