Coming from C#/.NET MVC 3/4, I'm not really used to CodeIgniter's implementation of models.
The documentation shows models being loaded within controller methods, however I'm using the model in almost every method and my model is storing data used across its methods in properties via its constructor.
Is there any reason NOT to instantiate the model in the controller constructor that I'm overlooking?
You can load model as per following ways also :
means if you have your model in any folder so using following code you can load model in controller.
$this->load->model('modelFolder/' . $this->controller . '_model');
For eg. :
if you have your model in folder named "modelFolder" then do like this :
class demoController extends CI_Controller {
var $controller = "user";
/* Local Constructor Will Be Overriding The One In The Parent Controller Class So We Need To Manually Call It. */
public function __construct() {
parent::__construct();
$this->load->model('modelFolder/' . $this->controller . '_model');
$this->load->model('common_model');
}
}
Hope it will help you...
There is no reason not to load the model for every controller activation. It could even be put in the configuration's autoload for all controllers.
The only reason not to always load it would be if many operations do not need the model. Then you could save a little bit of memory and time.
Related
I want to create a base controller class for all my controllers in Symfony, I am very new to Symfony, so don't be angry with dumb question. I am asking this question because I can't do something like this
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class AbstractController extends Controller
{
public function __construct()
{
//...... check access level
$user = $this->getUser(); //This is not working, I don't have access to the Controller(the base class) properties
}
}
Now one of my controllers
class UserController extends AbstractController
{
public deleteUserAction(Request $request)
{
var_dump($this);// this will dump an empty class that is not instance of Symfony\Bundle\FrameworkBundle\Controller\Controller
//.... delete user
}
}
What is the best way to do this? please...
EDIT....
What I really want to do is to check whether a user privilege level is enough to access a particular action(e.g. deleteUserAction()) in a particular controller(e.g. UserController), I have a class that attach privilege level to all actions in all controllers. The check will be very efficient if it happens in a parent controller (e.g. BaseController's constructor) which is executed before UserController->deleteUserAction() but in the base controller I don't have access to $this.
I have tried voter and ACL none help my situation.
Thanks in advance.
I think second one is the best way to create your own class and use common function in it.
If you want to add some common functions of controller then it is not the proper way to add it into the Symfony default controller, Instead you can create BaseController and extend your all the controller with BaseController and your BaseController should extends Controller.
By this way the default controller of the symfony stay untouched.
simply use service controller... is shared:
http://symfony.com/doc/current/controller/service.html
Is directly call to the Model class inside the View is best practice or not? Currently I am using CodeIgniter to develop an application. In different Views of my application I'm including menus that I want to pull from the database. And the thing is currently I am passing the values to the menu through the controller. If I make a common model class and call it from the View and by pass controller. So that there will be one call to Model and it will load menu from the database at once and by pass the controller. By doing this what pros and cons will come?
With codeigniter, your views should not be concerned as to where data comes from, only that it exists. Only your Controllers should be in direct contact with your Models.
It sounds like you have a common menu that you want to load in your views and you don't want to replicate that code across all your Controllers.
To solve this problem, you need to create a common controller that your primary controllers inherit from with a method that fetches the menu.
My_Controller needs to be saved to the core folder in the application directory.
class MY_Controller extends CI_Controller
{
protected function get_menu()
{
// Load your menu here
$this->load->model('menu_model');
return $this->menu_model->get_menu();
}
}
All your primary controllers will inherit MY_Controller
class Home_Controller extends MY_Controller
{
public function index()
{
$page_data = array('menu' => $this->get_menu());
$this->load->view('home/index', $page_data);
}
}
In my opinion no its not best practice. View should contact Controller and Controllers should retrieve data from Model where Model does all the logic. Controller suppose to be the glue or middleman of View and Model.
Controller passes the returned data into View and then you do your foreach loop or whatever to display it.
View should not be doing any logic. Retrieving data from database is somewhat logic.
It is a worst practise. Then why we have controller concept for MVC. :) Mainly all logics goes into Model so thats not good to call model directly.
There will not be any problem, since you are using a common controller.
Note : You should not call model directly from view.
I'm using CakePHP 2.3.0 and I need to be able to use an object (Logger) everywhere in my applications (from controllers and models).
This object is created in the AppController like this :
<?php
App::uses('Controller', 'Controller');
App::uses('Logger', 'Lib');
class AppController extends Controller {
public function beforeFilter(){
$this->Logger = new Logger(APP.'webroot/logs');
}
}
?>
This works fine for controllers, I can use this object from every controller of the application, this way : $this->Logger->...
But obviously, it doesn't work from a model (because models don't extends AppController).
So I need :
to know how to access a controller attribute (Logger) from a model
or another way to achieve this (creating a global object accessible everywhere), not using AppController->beforeFilter()
Thanks
To put it bluntly what you are doing is absolute crap. Read the chapter on logging on the cakephp manual and see how to configure log streams and how to write to them.
Just a quick logical question.
I have 2 Zend Controllers namely Merchant and Account. Merchant Controller has an action called editAction. My question is whether it is possible to have same action in Account as well without duplicating the code. I managed to create a include file for the view but I like to have a best way to manage my code. I currently have an idea of having an Helper class and invoke that helper class to both these Controllers. But I know someone would have got better solution than this. Please help me.
Thank you
The simplest solution would be to extend Zend_Controller_Action into your own base class and put editAction() into that. For example, assuming you have model classes named 'Account' and 'Merchant':
abstract class My_Controller_Action extends Zend_Controller_Action
{
protected $_modelName;
public function editAction()
{
$model = new $this->_modelName();
// Do your editing here.
}
}
class AccountController extends My_Controller_Action
{
protected $_modelName = 'Account';
}
class MerchantController extends My_Controller_Action
{
protected $_modelName = 'Merchant';
}
Keep in mind that this design implies that the code in editAction() would work for both Accounts and Merchants.
I think the best course of action would be to put majority of the logic inside your Models and keep your controllers lean. Your models can extend your own class which would have common operations in it.
You could also write your own class, it doesn't have to be a helper, and use it in the controllers to save the entity:
$saver = new My_Editing_Class();
$saver->edit("account",$this->getRequest()->getPost()); // Editing "account" with the POST data.
You then do your magic inside "My_Editing_Class".
I am building a series of forms, and I am trying to inherit the functionality of a parent Form class into all the forms. For example,
LeaveForm extends Form (Model)
LeaveFormController extends FormController
I am handling all the leave form specific stuff in LeaveFormController and LeaveForm.
In LeaveFormController constructor, I simply call the parent class constructor, then load the LeaveForm Model. And in FormController constructor, I load Form model.
My problem is, I get an error,
Cannot redeclare class form in Form.php
Have I got my architecture wrong? How do I handle this ?
check if the class has already been initialized like this:
if (!class_exists('classname'))
{
// ok fine create new instance now
}
Possibly when you $this->load->model('Form'), you manually included the models/form.php file?
In your leaveform.php model file, make sure you load the superclass model you extend using codeigniter's model loading mechanism instead of require or include. Codeigniter has a loader that keeps track of already-loaded files to avoid redeclaring classes, but you need to use $this->load to use it. It won't know about files loaded directly with include or require.
So at the top of leaveform.php, use this:
$CI =& get_instance(); $CI->load->model('Form');
This is not related, but you will have pain unless you namespace your CodeIgniter model classes the same way you namespace Controller classes.
Try using FormModel extends CI_Model {}; Instead of Form extends CI_Model {};