Skip to content

u/rbnc

In this archive from Apr 2010 to Jul 2017

7
posts
255
comments
1
media
953
score

Top posts

When it goes this far: use an if statement.

$this->uri != $this->real_uri ? $this->need_redirect = false : eregi("(cName|pName|mName)", $this->uri) ? $this>attributes['SEO_REWRITE_TYPE'] != 'cName' ? $this->need_redirect = true : $this->need_redirect = false : ereg("(".implode('|', $pattern).")", $this->uri) ? $this->need_redirect = true : $t...

45 points63 comments
Open on Reddit

Top comments

When I was starting out with MVC, no one explained it in a way I understood, I wish someone had written it out like this, (this is from a PHP MVC standpoint as per Cake, CI, rails): None of these examples will work in real frameworks they're just PHP psudocode.

Controllers.

Each controller is a collection of functions (aka actions or methods) called a class. The class' name usually relates to a database table, but not always. So for example, if you had a table in your database called 'page' in your database you might have a controller with various actions that a user can access related to that class (such as viewing, deleting and editing).

Your controller class has various actions or methods such as edit, view, delete etc. Each of these actions is a chunk of sub-code like any other function.

Example

<?php class page{ function view() { //view page code here } function delete() { // delete page code here } } Now, if you wanted to access the 'view' chunk of code on your 'page' controller, you would go to this url: www.yoursite.com/**page**/**view**/ That would tell your framework to load up the collection of blog functions then execute the one called view. You might have many different controllers with various actions within one website. Some frameworks offer an easy way to get variables into your code via the url. Let's say you wanted to load a page based on an database ID supplied within the url. In this example the ID is 31234 www.yoursite.com/**page**/**view**/31234 <?php class page{ function view($page_id) { //now we have a $page_id varibale which we can use to select certain stuff from the database } } Controllers never have direct database related code or html within them. Controllers are to react to user interaction (in this case sending of a url, but also cookies and GET/POST data) and based on that user information access various parts of the application such as database stuff (via a model, which we'll come to later), then pass that stuff into the views (we'll come to that later). GET,POST and URL stuff should only ever be accessed within the controller. **Models** Controllers sometimes are related to database tables. Models, almost *always* pertain to database tables. If we carry on with the 'page', example, i'll give you an example of a basic model. Again this is basic, pretend code. <?php class page_model{ function get_from_id($page_id) { $database->query("select * from pages where id=$page_id"); return $database->result; } function delete($page_id)) { $database->query("delete from pages where id=$page_id"); } function get_all() { $database->query("select * from pages"); return $database->result; } } As you might be able to tell, the model is just a set of actions related to manipulating a certain type of data. In this case we're manipulating a database table. Models should never have HTML or access GET or POST data. Models should be the only place you access your database from in your application specific code. **Models and Controllers** This is how the relationship between a model and controller might look. <?php // this is our controller class page{ function view($page_id) { $page_data=Page_model->get_from_id($page_id); } } If a user went to the url www.yoursite.com/page/view/453 Firstly the page controller would be loaded, then the view function would be executed with the variable from the url (453) set as a parameter. Then the parameter passed into the page model and the page database record returned. **Views** Views are your presentation data, mostly HTML. Views should never access the database or models. Views should never access get or post variables or cookies and should only contain the most basic of PHP functionality such as echo and loop control structures. I don't really need to show what a view looks like because it's just HTML and PHP, but here's an example of what one might look like <!-- a_lovely_view.php --> <title><?php $page_title;?></title>
    <h1><?php echo $page_heading;?></h1>
     <?php echo $page_content;?>

The final part of the jigsaw is to get variables from the controller, into the view

Models, Controllers and Views*

<?php
       // this is our controller
      class page{

         function view($page_id)
         {
             	$page_data=Page_model->get_from_id($page_id);
               
	$data=array(); //create a blank array
               $data['page_title'] = 'Hello everyone';
               $data['page_heading'] = 'This is the heading';
               $data['page_content'] = 'The Page ID requested was '.$page_id;

               // now push these variables using an array into the view we made above 
               view('a_lovely_view.php',$data);
          }
}

So if our user went to

www.yoursite.com/**page**/**view**/1234

the result would be

    <title><Hello everyone</title>
    <h1>This is the heading</h1>
     The Page ID requested was  1234

I hope that simplifies this for you. As an absolute beginner, I'd suggest CodeIgniter because its very, very simple.

29 points

Things I'd love to see implemented in the near future.

  • Ability to hint to methods/functions. To avoid stuff like: createimage(null,null,null,null,null,100,100); and allow: createimage([x:100,y:100]);

  • A String object.

  • A way to create short object

    $hello={'name':'robin','age':12,getWeight:someCallback}; echo $hello->name;

18 points

Subreddits

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.