How to build CodeIgniter site correctly - php

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.

Related

Mini CMS - need some advices on pages

I am going to write a super small cms with https://github.com/panique/mini/
Now I want to add a small pages section in the Admin of the site (this can be done easily ).
The advice part comes here:
The url of the mini framework is mostly easy, its /controller/method ( if its the index method then it won't needed to be shown in the url ).
So there is a file which checks if the controller is existing so it can load it.
But the thing is an user is not going to create a controller every time after creating a page.
What would be the best approach to do this ?
This file is checking if that controller exist: https://github.com/panique/mini/blob/master/application/core/application.php
Thanks in advance
The way this handles routing, is much like CodeIgniter. The path would be controller/method/arg1/arg2/arg3..., so you can write a controller, define a method which accepts one (or more) arguments to load user pages.
Assume, user has created a page named news. He/She may load the page via URL pages/view/news.

Switching from Procedural to OOP PHP using Code Igniter

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

Laravel Automatic Adding Resource Directory to URL?

I need some help. I'm new to laravel and I'm experiencing some weird things.
I have a "users" resource inside views/users. Inside it are index.blade.php andcreate.blade.php.
My app url is http://laravel.dev/ so basically if I need to access the "Create Users" form I will go to http://laravel.dev/users/create the view loads fine but when I tried to click a link on my navbar it automatically adds "users" on the url even if there's no "users" in the nav url link. Example.
Dashboard
When I click it, it will add "users" in the beginning of the url automatically making it users/dashboard and as expected I get a 404.
My navbar is being loaded from other php file in my header.php
Here's my route
/*Route for Users*/
Route::resource('users', 'UsersController');
I would suspect that you are using page relative links, correct? In a nav I've always found it best to use root specific links so "/dashboard" vs. "dashboard."
If you are new to Larvel you may want to take a serious look at the rooting options before going to far into your project just because if you decided to go a different way, namely routing through the controllers (http://laravel.com/docs/controllers#resource-controllers) With a large app it can be a far better way to go, but the refactoring can be tough after to you have a complex enough app to justify it.

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?

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