Switching from Procedural to OOP PHP using Code Igniter - php

I'm finally making the switch to OOP from procedural and starting with CodeIgniter and just looking for clarification. For every page that you would typically have (about.php) will you have both a view (about.php) and a controller (About.php) for every page? I'm trying to wrap my head around it and I've always given up and went back to procedural and now I'm forcing myself to learn object oriented.

That's certainly one way you could do it, but it doesn't have to be that way.
In CodeIgniter, your controller forms a section of your site, and looks in most URLs like a sub-folder, e.g.
http://example.net/products/tvs
There, products is the name of a controller, and tvs - in URL terms, the sub-page - is a method of that controller.
So yes, you could have a controller for each page, but no, you normally wouldn't. Think instead of controllers being responsible for site sections rather than individual pages.

The controller controls everything, you could have all your info in a DB, but if you want to have actual files, then put them in your "view" folder and just add a function in your controller.
Something like this:
controller:
function about_us_page() {// load your about_us.php view file here}
view:
about_us.php
and you should link to it like:
example.com/controller/about_us.php

Related

PHP MVC - Where/when to load shared page content ('modules'/'widgets')?

I'm trying to build a simple PHP MVC framework. What I have right now is a basic set-up with a router that sends the request to the right controller, so I'm making the URL
/blog/[id]
Resolve to a class method like
Blog->singlePost();
Within singlePost(); I then interact with the model to get the right blog post and send it off to the view. The basic MVC triad is all there.
But pages are more complex than this. I've got a sidebar that I need to fill with data (from the database and other sources). I've got a main menu to build, I need to show the details for the current logged in user, and more. But, that said, I don't need all of these sections on every page.
My question is, where do I initiate/build all these various shared sections for the view?
My options that I can think of are:
Have every controller send a request for the required additional sections to be built (would have to update every controller each time something is added, no way)
When mapping the URL's, define a group that the route belongs to that loads in all the required sections (Not ideal, not every section will be needed on every page, could get messy)
Have the View (or Template) embed these sections on-demand. So the data for these sections would not be retrieved until the view explicitly asks for the data (The basic functionality of this seems quite appealing, but does it break the rules of what the view should do?)
Something else?
3 Seems quite appealing to me because I'd like for my templates (that will be dynamic and switchable via a template system) to be able to request a section on any page. So one template may show the "Top 10 Blog Posts" on the homepage, but another may not, so there's no point in loading it. I see that with Symfony/Twig, you can do this: http://symfony.com/doc/2.0/book/templating.html#embedding-controllers
Is this a good idea for various modules on a site? Or should everything be loaded before the view starts being processed?

Codeigniter Dynamic Routing to single controller

I know this question has been asked in a similar fashion several times. However, I'm struggling to find any answers that would work in my situation. I primarily work on Microsoft projects and stepped in on this project to help during crunch.
Here's the situation.
We have a client who has a site with over 600 different pages. In reality each page uses the same template just populates with different data. We've developed a CMS for him which allows him to create new pages at will.
My ideal solution would allow me to store the name of a newly created page in a DB.
Ex. new_page_1 has been created and now exists in the DB. Now when I type in www.mysite.com/new_page_1 this needs to go to a controller that looks up "new_page_1" (in DB) and if it exists loads a view (THIS VIEW WILL NEED TO BE USED FOR ALL 600 pages) which then takes other data from the DB and populates various sections.
So essentially, over 600+ pages need to use the same route array and map to the same controller which then maps to the same view.
I've tried using $route['(:any)'] = 'custom_controler/create/$1 and as well as the same array key but using main and _remap. No matter what every single time it tries to look for the page name in my views (which it will never exist because I'm using one generic view for 600 pages)
Any ideas on how to accomplish this?
UPDATE
routes.php (this is the last line in the file)
$route['(:any)'] = "main/create/$1";
main.php (controller)
class Main extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function create($page)
{
$c = new Category();
$c->get_by_name(ucfirst($page));
$this->load->view('site/index',$c);
}
}
the URL I'm attempting is sitename.servername.com/health sitename and servername obviously substituted.
The error I get is
An Error Was Encountered
Unable to load the requested file: health/main/create.php
Is the error page you're seeing the CodeIgniter error template or a generic server error? That error string sounds very much like you are using Apache or Nginx (or whatever main webserver you use) and its actually not even resolving to your CodeIgniter app at all, but searching for a PHP file that doesn't exist. You'll probably need to use mod_rewrite or something like that to make that URL point at the CodeIgniter install.
Otherwise, your implementation doesn't look completely wrong: you probably need to make sure main.php is the default route as well.
Your application will only look for the page name in your views if you tell it to. Your catch-all controller method should be checking the database for a valid page name, and then regardless of which pages validate, loads the same view (albeit with different data passed to the view).
Using an (:any) catch-all route is perfectly fine. Your controller code somewhere is what's throwing you off. Update your post with the code if you continue to struggle.

How to build CodeIgniter site correctly

I'm building a website using CodeIgnitier.
My problem is how can I use the MVC pattern inside a webpage.
Let's say I have a controller called "Settings", Within this page I want to divide the settings into categories, with tabs, like "System", "User" and so on.
I know how to do this in the usual way, something like "settings?tab=system" and then load the proper view according to the tab selected. But I want to make a separate MVC pattern for this settings page, is it possible? and how?
Basically the thing you are looking for is can be controlled using routes.ini or Route.php. The tab can be your querystring. Settings can be a controller and u can have an action called index which can be controlling it.

Custom tags for my very small cms (Codeigniter)

I'm developing a very small cms kind of thing. So far I have a page controller that lets you add pages. And there is a frontend controller. All calls are received by the frontend controller and respective page is displayed. Now I want to allow some custom tags while creating a page. I am using CKEditor and I want that while creating a page user gives a tag like <!--cmsform_printform--> and this tag will print printform method of cmsform controller.
For this I have created a controller cmsform that has a method printform. In my fronend controller I have created a method replace_tags that searches tags and then call respective controller and method.
But now I realized that my logic was false because I should not load a controller in another controller. I can't think of anyother logic. Please someone guide me or redirect me to a good codeigniter tutorial that explains this.
Thanks
Have you tried writing a helper instead? The functionality you describe sounds more like a helper method than a controller action.
CI manual in their site lists several excellent video tutorials, notably the ones published on NetTuts+. You might want to have a look, if you haven't already. Also, please do search CI forums. They contain plenty of information on pretty much any CI-related topic.

Combining multiple controllers on one page, without loading controllers from a controller

I'm trying to create a page that displays certain widgets/includes dependent on the users permissions. I'm quite new to CodeIgniter and MVC.
Example.
I have a calendar. This has a controller, model and view and it needs to be included on other pages.
I want this calendar and a number of other similar 'widgets' to be displayed on one page. This page currently has a controller and view.
I know I shouldn't be loading a controller from another controller, but can't see a way round it.
Can you load multiple controllers from a helper, or do I need some form of template?
Thanks,
Jacqui
It sounds to me like you are looking for Modular Extensions for CodeIgniter. This allows you to break an application into modules that can be called from one another. In your case, you would have a calendar module which you could call from your other modules.
What you might be looking for is something called Layouts. Codeigniter does not have such a feature out of the box but layouts are basically delegating a (front-controller) request across multiple actions. Each action works on it's own and has it's own view assigned.
The layout will take care to put this all together into one output then.
This methodology takes care as well that you don't call one controller from another controller so you keep code apart that does not belong to each other or would get messed up because it does too much at once.
This discussion might be interesting for you: Codeigniter layouts?, the Codeigniter FAQ has more about it as well and more specifically Header and footer and menu on every page in the wiki.

Categories