I have a problem with PHP/MVC.
This is my index controller.
<?php
class Index extends Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->view->render('header');
$this->view->render('index/index');
$this->view->render('footer');
}
public function test(){
$this->model->test();
}
?>
And this, index model.
<?php
class Index_Model extends Model{
public function __construct() {
parent::__construct();
}
public function test() {
return $this->db->select('SELECT text FROM test');
}
}
}
How can I use this in "index view"?
Of course you can call controller and model methods from your views (if it wasn't possible then you'd need to put EVERYTHING in your view). The idea of MVC isn't complete separation (as some people wrongly think) but separation of the logic. This means that it is fine to pass data between the different parts of the MVC arch; the idea is that you want to try to make sure that the logic in each part is logic that is appropriate for that part.
Many people will over complicate this in search of some ideal which is just silly. I would get rid of the controller method all together and reference the model method directly in the view if all you are doing is grabbing some data to be displayed in the view. There is no reason to waste time and effort passing these results to the controller just to be passed back to the view. You will thank me for this advice once you make a truly large site and look at all of the effort you would have spent doing that (and then later maintaining all of it).
My philosophy is that a controller should be used for just that (controlling the application logic). If you want to display a sidebar with a list of the most recent post that has NOTHING to do with the controller so it shouldn't add any code to it. Save yourself some headache and call the model from the view. In the end your code is going to make more sense if you do.
In response to your question: You cannot use your controller method in the view because it doesn't return anything.
Related
Mostly this question is not about programming, it's like the how to use question.
and as I said above, should i make a class for every page in codeigniter? Or I can make function's for every page? if both is right, which one is better?
If you want to make sections/pages like.. "/widgets/view/1" or "/widgets/edit/1" or "/widgets/delete/1" your code would be:
class Widgets extends CI_Controller {
public function view($id)
{
//Go get widget by id $id
}
public function edit($id)
{
}
public function delete($id)
{
}
}
Please check CI Routing http://ellislab.com/codeigniter/user_guide/general/routing.html
You can use 1 controller class with 20 functions for each page or you can have 4 controllers with 5 pages etc...
It depends on the complexity and similarity of your pages. If they are really alike, it could be a waste to create lots of classes, but in general I would create a class per page at least.
i am writing a web application and i need all the data returned/computed in all actions of one specific controller to return data to the layout (not the view).
So after each action, a controller variable needs to be passed to the layout so the layout can use it.
In detail, i want to store the calculated data in jSon in the head.
Any ideas anyone of how to do this?
I thought about a controller plugin but i have no idea of how to access the desired parameters then and i really don't want to use a singleton for all this.
Let use, this method is executed before every action
public function preDispatch() {
}
for example
public function preDispatch()
{
//calculate something
//this is an example
if($this->getRequest()->getActionName()=="admin")
{
$this->_helper->layout->setLayout('admin');
}
else
{
$this->_helper->layout->setLayout('user');
}
}
I'm using the MVC pattern in my application.
Now I need the view object in a model.
I don't want to add the view as a parameter for my function in the model (since I need it in other functions as well). And I don't want to keep on passing it.
Should a add the view as an attribute for the constructor of the model?
Is there another way? Shouldn't I be needing the view object in the model in the first place?
What would be the preferred way of doing it?
Example:
Controller
function someAction()
{
$somemodel->add();
}
Model
class SomeModel()
{
function add()
{
if ($view->user) {
// do stuff
$this->mail();
} else {
// do other stuff
}
}
function mail()
{
Mailer::send($view->user->email, $this->getitems(), $view->layout);
}
function getitems()
{
return Items::getitems($view->user);
}
}
If you're really doing MVC, then you won't need the view in the model, because only the controller should have access to the view.
Looking at the code you've provided, I can tell one thing: the add() method should not reference $view in any way (even for accessing its properties). Instead, the model should be provided with the $view->user value from the controller. The same goes for the mail() method.
Consider fixing those issues. Otherwise, you'll get into something worse later on.
The model should be separate from the view. So, as mkArtak said, the controller should be the only thing that communicates with the view. Which then passes only the necessary information to the model.
As for the model, it should really only deal with the information that it understands.
i.e. if you had a Car model... you don't want to build it dependent on it's factory. If you did, you would have to change your code if you wanted to build it in different factory.
The controller is where you 'bake' everything prepare for render. By bake I mean you consider any passed in $_REQUEST params, make model API calls to get the data you need, and set template variables to be rendered. Your action, at the end of this process should make a call to a template (view) you choose in order to render the 'baked' template variables.
EDIT: Previous title, because no one was reading the question: "If I'm making AJAX calls in an MVC framework, is it common to have 'getter' controller methods over the model?"
It's all in the title: If I want to make an AJAX call to delete a user, that DB code clearly lives in the model $this->userModel->delete($id).
If I'm making all of the CRUD calls via AJAX, do I just have passthrough controller methods to expose those model calls to a URL?
function delete($id) {
$this->userModel->delete($id);
}
etc? This seems silly, but it also makes sense... but it also makes me feel like I'm missing something. Is this the most common pattern?
Thanks!
When it comes down to Ajax under the MVC Framework I usually tend to have each function with a specified keyword such as fetch / set.
something like this
class Users extends Controller
{
public function Fetch($id){}
public function Create(){}
public function Update($id){}
public function Remove($id){}
}
To answer your question: yes
The task of the controller is the decision maker, so that you would perform authentication checks etc within the controller for security purposes.
Think of it this way, you would not use the same controller for changing records in your administration as you would within your users front-end, but you would use the same models.
the models should be used in more places than just the front end, so you would not place a session check, input validation in the model methods as you would perform different checks depending on the location the action is taking place.
Your frontend controller would have something along the lines of:
public function Fetch($id)
{
if($this->session->get_userdata("auth_level") & USER_AUTH_LEVEL_READ)
{
//Show data
}
}
where as your administration would have:
public function Fetch($id)
{
if($this->session->get_userdata("auth_level") & IS_ADMINISTRATOR)
{
//Show data
}
}
if you place the very same check in your models then you would have generate several models that would do return the same data regardless of the location.
Ok so I have some gaps in my understanding of PHP OOP, classes and functions specifically, this whole constructor class deal. I use both Zend and CI but right now Im trying to figure this out in CI as it is less complicated.
So all Im trying to do is understand how to call a function from a view page in code igniter. I understand that might go against MVC but Im working with an api and search results not from my database, so basically, I want to define a function in my class that I am able to call in one of my view pages.. and I keep getting "Fatal error: Call to undefined function functionname" error no matter what I try.
I thought I just had to declare
public function testing() {
echo "testing testing 123;
}
but calling that from the view I get that error. Then I read something about having to go
parent::Controller();
in the index of the class where the testing function also resides? But that didnt work either. Anyways, ya, can someone explain what I need to do in order to call the "testing()" function on one of my view pages? and clarification on the constructor class and what exactly parent::Controller() even does, would be much appreciated as well.
Like you said, that goes against the concept of MVC, so a better bet would be to use a helper function instead of declaring it as a controller method. Or, even better, let your controller deal with all the search API stuff, then pass the search results from your controller to your view.
I agree with both those points, but there are instances where you don't get the data you need until the view has been loaded (e.g. like some php data inside inline javascript or something).
If that's the case, I'd use an ajax call (within the view) to hit a function in the controller (since you just need a url to call them) and send along post data if the function needs to be fed anything. Does that make sense?
Yeah, like you said, that goes against the concept of MVC, but nothing is impossible. We can make it simply and follow the CI concept. You just need to pass a Controller object to the view:
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function sayhi() {
echo "Hi from controller...";
}
public function index() {
$this->load->view('test', array('controller' => $this));
}
}
Then you can call function defined at the controller, for example I called sayhi() from my view page:
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h2>
<?= $controller->sayhi() ?>
</h2>
</body>
</html>