I am doing a simple Web application which works more or less like this: (simplified)
Module Selection page: When the user
selects a module, the module page
appears
Module page: Contains buttons for
various items. Click on a button and a
small AJAX script retrieves the item
for a Module Handler
Module Handler: simple PHP script called from the module page through AJAX.
Does the job by querying the database and checking permissions
My problem is with the issue of the first item: When the user lands on the Module page, the first item should be already displayed, but all the code to retrieve the item is in the Module handler.
I could of course import the Module Handler inside the Module page and query the functions directly in PHP to get my first item, but it feels a bit dirty to include the handler, and then call to it.
Is there a better way of doing this?
If you are looking for a good design pattern for a web application, look no further :
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
The Model View Controller is a well tested one and various frameworks use it.
In your case, the modules are the controllers, the pages are the views and the handlers are the models.
Well i can simply suggest let your first view be loaded, be blank. Just do a include to the default module you want to handle. That way, the view will be just playing like a router. Rather then routing it to the request, it will just import it and use it.
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 have a folder containing a php booking system,that i want to render as un article and add it to the main menu of the site.
The solution that i end with, is to use the joomla URL Wrapper. But it brokes my design as it render it inside an iframe.
Is there another way to do that?
Use Jumi, it includes PHP files into articles, without iframes.
Usage:
{jumi [path/to/file.php]}
Jumi website
That requires an integration work. At least you'd rather think of the way to give some Jooml Component wrapper to your booking sysytem. which at least involves creating a com_yourcomname folder, and a yourcomname.php that will dispatch your actions to the actions of the booking component and, perhaps, filter the urls in the output so that they match the Joomla url building convetions. If do this properly, your app will appear as a page content (not an article)
My codeigniter app is multilingual and I want to redirect users to their pages, by checking IP address.
I should check it at the top of all pages (i know i can set a session or cookie, but i want to check it on all pages); before any views or other.
Where should i put my code (function)? on Startup file or Loader? or create an extension or plugin and load it on Startup? if it can be done by an extension or plugin, how can I create it? (i've searched, but didn't find a useful tutorial)
Thanks.
If you're using a main front controller you can put your code in there. But a better way to do it would be to use CodeIgniters built in functionality to extend the core - hooks!
http://codeigniter.com/user_guide/general/hooks.html
Just select the point you want your script to be activated and take it from there.
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.