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.
Related
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.
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?
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.
I'm currently building a CMS for a project in codeIgniter and I was wondering if there's any way to create dynamically a controller file in codeIgniter via the CMS when the user create a new page in the database. I don't need to create another model because the tables in the database will be the same, as well as the view. Basicly, all the page in the website (link, title, content etc.) are from the same table. I just want to keep the uri like this (www.mywebsite.com/class/function). Also no routing.
I was looking around the web and the only thing I found close of my request is the CRUD "codegen", but i can't use it online and there's alot of things I just don't need from it.
While it is possible to create a new controller each time the user adds a new page to the database, would it not be better to have a single controller which takes the db id or page name as parameter and loads that page?
So if the user created two pages called page1 and page2, and you have a controller that is used to serve all dynamic pages named dynamic.php then the urls would be
www.mysite.com/dynamic/page1
www.mysite.com/dynamic/page2
Creating a controller each time the user creates a page is a big overhead, and also would require lots of code to properly implement, like deleting the controller when the user deletes the page from database, for instance. And I think to provide a coding solution within SO would be cumbersome.
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.