Asked  7 Months ago    Answers:  5   Viewed   37 times

As a mostly self-taught programmer I'm late to the game when it comes to design patterns and such. I'm writing a labor management webapp using CodeIgniter.

I did a bit of MVC ASP.NET/C# and Java at school, and the convention was that your model mainly consisted of classes that represented actual objects, but also abstracted all the database connection and such...which is pretty standard MVC stuff from what I've gathered.

I imagine I'm right in saying that CI abstracts the database connection completely out of sight (unless you go looking), which is par for the course, and the models you can create can abstract the 'generic' CRUD methods a bit more to create some methods that are more useful for the specific model.

The thing I have a problem with, because it's different to what I'm used to with MVC, is that whenever you say...return a row from a database, the convention is to just put it in an associative array or standard object with the properties representing the data from the row.

In ASP you'd have actual classes that you could construct to store this information. For example you'd have a House class and the data would be stored as properties (e.g. bedrooms, bathrooms, address) and the methods would represent useful things you could do with the data (e.g. printInfo() may print("$address has $bedrooms bedrooms and $bathrooms bathrooms!')).

I'm getting the impression — just from code I've seen around the Internet — that this isn't the standard way of doing things. Are you supposed to just use arrays or generic objects, and say...do $this->house_model->print_info($houseobject) instead of $houseobject->print_info();?

Thanks.

 Answers

91

Despite its claims to the contrary, Codeigniter does not use MVC at all (In fact, very few web frameworks do!) it actually uses a PAC (Presentation-abstraction-control) architecture. Essentially, in PAC the presentation layer is fed data by a presenter (which CodeIgniter calls a controller) and in MVC the View gets its own data from the model. The confusion exists because of this mislabeling of MVC.

As such, CodeIgniter (and most of the other popular web frameworks) don't encourage a proper model but just use very rudimentary data access its place. This causes a large set of problems because of "fat controllers". None of the code which relates to the domain becomes reusable.

Further reading:

  • MVC vs. PAC

  • The M in MVC: Why Models are Misunderstood and Unappreciated

  • Model-View-Confusion part 1: The View gets its own data from the Model

  • The Fat Controller

This is a widespread point of confusion in the PHP Community (The MVC vs. PAC article identifies the problem as stemming from the PHP community. that was written in 2006 and nothing has changed, if anything it's got worse because there are more frameworks and tutorials teaching an erroneous definition of MVC.) and why people looking at "MVC" PHP code who have come from a background outside web development understandably become confused. Most "MVC" implementations in PHP are not MVC. You are correct in thinking models should be properly structured, it's CodeIgniter that's wrong to label itself as MVC.

Wednesday, March 31, 2021
 
CBroe
answered 7 Months ago
23
<form method="post" action="<?php echo base_url();?>controller/save" name="form">
  <label>User Name</label>
  <input type="text" name="Username">   
  <input type="submit" value="Add"/>
</form>

in controller

function save
    {
    $arrData["Username"] = $this->input->post("Username");
    $this->User_model->Add($arrData); 
    } 

in User_model

function Add($arrData) { 
        if($this->db->insert(tableName, $arrData))
          return true;
        else
          return false;
    }
Saturday, May 29, 2021
 
superhero
answered 5 Months ago
14

each_with_object does not work on immutable objects like integer.

(1..3).each_with_object(0) {|i,sum| sum += i} #=> 0

This is because each_with_object iterates over a collection, passing each element and the given object to the block. It does not update the value of object after each iteration and returns the original given object.

It would work with a hash since changing value of a hash key changes it for original object by itself.

(1..3).each_with_object({:sum => 0}) {|i,hsh| hsh[:sum] += i}
#=> {:sum => 6}

String objects are interesting case. They are mutable so you might expect the following to return "abc"

("a".."c").each_with_object("") {|i,str| str += i} # => ""

but it does not. This is because str += "a" returns a new object and the original object stays the same. However if we do

("a".."c").each_with_object("") {|i,str| str << i} # => "abc"

it works because str << "a" modifies the original object.

For more info see ruby docs for each_with_object

For your purpose, use inject

(1..3).inject(0) {|sum,i| sum += i} #=> 6
# or
(1..3).inject(:+) #=> 6
Thursday, July 29, 2021
 
FyodorX
answered 3 Months ago
35

RedirectToRoute returns a RedirectToRouteResult. Try this instead.

return RedirectToRoute("BetaAccess");
Friday, July 30, 2021
 
zerzer
answered 3 Months ago
58

You need to scan your /application/controllers directory and remove file extension from it

    $controllers = array();
    $this->load->helper('file');

    // Scan files in the /application/controllers directory
    // Set the second param to TRUE or remove it if you 
    // don't have controllers in sub directories
    $files = get_dir_file_info(APPPATH.'controllers', FALSE);

    // Loop through file names removing .php extension
    foreach ( array_keys($files) as $file ) {
        if ( $file != 'index.html' )
            $controllers[] = str_replace('.php', '', $file);
    }
    print_r($controllers); // Array with all our controllers

OR

You can also follow this link to achieve this

controller list

Friday, August 20, 2021
 
kat_indo
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share