Welcome Guest, Not a member yet? Register   Sign In
Best Practice Regarding Controllers / Site Structure
#1

[eluser]JamesTaylor[/eluser]
OK, so i have almost completed building my 1st site using codeigniter and an MVC structure. The site is only a small one for a local tooling company and i have used it as a live project in order to learn CodeIgniter.

The site's structure is broken down as follows:

Home Page - 1 page

About Us - 1 page

New Tooling - 6 sub pages
Indexable Tooling
Milling & Drilling
Work Holding
Spindle Tooling
Threading Tools
Inspection & Metrology

Used Tools - 1 page

Offers - 1 page

Contact Us - 1 page

What is considered the best practice for use of controllers in the above the example? I have developed the site using a single controller called 'structure' and defined each page within it. I did it this way as my site is only small it was easy to have all the controller functions in place.

However i am now thinking that perhaps the appropriate way would be to have a controller for each main section
i.e Home / AboutUs / New Tooling / Used Tooling / Offers / Contact Us

If i did as outlined above how would i deal with the sub pages of the New Tooling section? would i include these as sub functions in the New Tools controller or should each sub page have its own controller?

Thanks guys, looking forward to your advice!

James
#2

[eluser]jedd[/eluser]
Hi James,

If the controller is under a thousand lines, I'd stick with the single controller approach - especially if you don't anticipate the codebase growing.

As to the mythical best practice here - the mantra you need to repeat, as you doze off each night, is 'one controller per resource'. Of course this line can be fuzzy.

For static stuff (home, about us, t&c's etc) I think a lot of people tend to put those pages under one controller. Of course, much depends on how that data is stored and updated (in- or outside of the application proper).

If used tools runs a different method and/or is substantially different from the new tools / products page, I'd possibly split that up. Similarly the offers page (a kind of forum?).

The sub-tools stuff sounds like something you'd keep within the same controller - it's just slightly different takes on similar sets of data, isn't it?
#3

[eluser]Udi[/eluser]
I would make "page" controller, that loads content from the DB.

I don't think creating methods for each page is good idea even if its a small amount of pages.

Create new table called pages:
ID,
ParentID - New Tooling for example,
Title
Content
UrlTitle - for cool urls like example.com/page/Inspection_and_Metrology,
The UrlTitle is the identifier of the page in your site, not the ID.

Write a model that retrieves you content of page by the UrlTitle segment... and you're good to go.
#4

[eluser]JamesTaylor[/eluser]
In this instance the site doesn't actually use any methods, i was planning on the use of a database so i could have a go at implementing that side of things with CodeIgniter but it ended up the client didn't want to display enough info for it to be worth while. I have a couple of other things in the pipeline that will allow me to play on that front...

...Udi your post has just come as i am replying to Jedd's post - are you suggesting i place all the content of each page, even if it is static, into a database?

What i have done in respect to my controller called structure which has everything incorporated into it for the entire site is set out below.

Code:
class Structure extends Controller {

//Home Page
    function index(){
    $data['main_content'] = 'home';
    $data['title'] = 'Welcome to RDBarrett - New & Used Engineers Tooling';
    $data['h1'] = 'Welcome to RDBarrett - New & Used Tooling';
    $data['h2'] = 'Welcome to RDBarrett';
    $data['BodyID'] = 'HomePage';
    $data['ToolingID'] = 'ViewRangeLinks';
    $this->load->view('template', $data);
    }

//About Us    
    function about_us(){
    $data['main_content'] = 'about_us';
    $data['title'] = 'About RDBarrett - New & Used Engineers Tooling';
    $data['h1'] = 'About RDBarrett - New & Used Tooling';
    $data['h2'] = 'About RDBarrett';
    $data['BodyID'] = 'AboutUsPage';
    $data['ToolingID'] = 'ViewRangeLinks';
    $this->load->view('template', $data);
    }
    
//New Tooling
    function new_tooling(){
    $this->indexable_tooling();
    }

//Indexable Tooling
    function indexable_tooling(){
    $data['main_content'] = 'indexable_tooling';
    $data['title'] = 'Indexable Tooling - RDBarrett';
    $data['h1'] = 'Indexable Tooling Suppliers';
    $data['h2'] = 'Indexable Tooling';
    $data['BodyID'] = 'NewToolingPage';
    $data['ToolingID'] = 'IndexableTools';
    $this->load->view('template', $data);
    }
    
//Milling and Drilling
    function milling_drilling(){
    $data['main_content'] = 'milling_drilling';
    $data['title'] = 'Milling & Drilling Tools - RDBarrett';
    $data['h1'] = 'Milling & Drilling Suppliers';
    $data['h2'] = 'Milling & Drilling Tools';
    $data['BodyID'] = 'NewToolingPage';
    $data['ToolingID'] = 'MillingTools';
    $this->load->view('template', $data);
    }
    
//Work Holding
    function work_holding(){
    $data['main_content'] = 'work_holding';
    $data['title'] = 'Work Holding Tools - RDBarrett';
    $data['h1'] = 'Suppliers of Work Holding Tools';
    $data['h2'] = 'Work Holding Tools';
    $data['BodyID'] = 'NewToolingPage';
    $data['ToolingID'] = 'HoldingTools';
    $this->load->view('template', $data);
    }
    
//Spindle Tooling
    function spindle_tooling(){
    $data['main_content'] = 'spindle_tooling';
    $data['title'] = 'Spindle Tooling - RDBarrett';
    $data['h1'] = 'Spindle Tool Suppliers';
    $data['h2'] = 'Spindle Tools';
    $data['BodyID'] = 'NewToolingPage';
    $data['ToolingID'] = 'SpindleTools';
    $this->load->view('template', $data);
    }
    
//Threading Tools
    function threading_tools(){
    $data['main_content'] = 'threading_tools';
    $data['title'] = 'Threading Tools - RDBarrett';
    $data['h1'] = 'Suppliers of Threading Tools';
    $data['h2'] = 'Threading Tools';
    $data['BodyID'] = 'NewToolingPage';
    $data['ToolingID'] = 'ThreadingTools';
    $this->load->view('template', $data);
    }
    
//Inspection Tools
    function inspection_metrology(){
    $data['main_content'] = 'inspection_metrology';
    $data['title'] = 'Inspection & Metrology Tools - RDBarrett';
    $data['h1'] = 'Suppliers of Inspection & Metrology Tools';
    $data['h2'] = 'Inspection & Metrology Tools';
    $data['BodyID'] = 'NewToolingPage';
    $data['ToolingID'] = 'InspectionTools';
    $this->load->view('template', $data);
    }
    
//Used Tooling
    function used_tooling(){
    $data['main_content'] = 'used_tooling';
    $data['title'] = 'Used Tooling & Machines - RDBarrett';
    $data['h1'] = 'Suppliers of Used Tools & Machinery';
    $data['h2'] = 'Used Tooling & Machines;';
    $data['BodyID'] = 'UsedToolingPage';
    $this->load->view('template', $data);
    }
    
//Offers
    function offers(){
    $data['main_content'] = 'offers';
    $data['title'] = 'Tooling & Machine Offers - RDBarrett';
    $data['h1'] = 'Engineers Tooling & Machinery Offers';
    $data['h2'] = 'Tooling & Machines Offers;';
    $data['BodyID'] = 'OffersPage';
    $data['ToolingID'] = 'ViewRangeLinks';
    $this->load->view('template', $data);
    }

Part of my reason for asking is with one eye on the end urls that are displayed in the address bar (for SEO purposes as well) - i'm looking into htaccess files and mod rewrite but again its something i'm not familiar with at the moment.

All url currently show:

/RDBarrett/index.php/structure/...

i have implemented a simple htaccess file with code copied from another post on here to make it

/RDBarrett/structure/...

but i still want to get rid of the '/structure' part as it isn't friendly! If i had a controller for each main section it would create good urls (easily with current htaccess file) for users and SEO such as:

/RDBarrett/About_Us
/RDBarrett/New_Tooling
/RDBarrett/New_Tooling/Milling_Drilling - the Sub pages of New Tooling could be incorporated into that controller.

I appreciate i am asking about two issues that can be dealt with separately but at the moment i am seeing them as being inter-twined! as a noob i'm just trying to get my thoughts together
#5

[eluser]Udi[/eluser]
For the URL part -> use route array, in routes.php.

Now, I suggested to use DataBase because it makes a lot more sense, even in small sites.
If those are static pages, then cache them for 24 hours, or even more.
#6

[eluser]Omar Vazquez[/eluser]
I usually setup one controller per resource. Any resources that have sub pages with children would be passed as method parameters. For example:

Code:
class Portfolio extends Controller{

    function index()
       {
          landing page code...
       }

     function digital($type='web')
     {
        switch($type)
          {
             case 'web':
                code here...
             break;

             case 'kiosk':
                code here...
             break;
          }
     }
}

This would yield urls like this:

www.site.com/portfolio/digital/web
www.site.com/portfolio/digital/kiosk

where "portfolio" is our controller, "digital" is our method and "web" and "kiosk" are parameters that get passed into our digital method.

See this entry in the user guide for more details on passing uri segments to your methods:
Passing URI segments to your methods

Hope that helps a bit.

Omar
#7

[eluser]JamesTaylor[/eluser]
Thanks for the suggestions guys,

been run off my feet so far this week but will be getting back to look at this as soon as i get chance!




Theme © iAndrew 2016 - Forum software by © MyBB