Suggestions for website template/design - Backend - php

I'm creating a website using Codeigniter which hosts online novels/e-books. The novel(s) have multiple chapters similar to a hard copy. Im planning to design the layout as following.
User goes to chapter 1, so the URL would be site.com/novel/chapter1/pageno
I plan to create a novel controller which has chapter1,2.. etc as its functions. The chapter function receives an input and gets the same form either a database or from a text file. I'd also want to store user progress which can be resumed later. I plan to use the chapter/pageno as the reference for where the user currently has stopped.
I'd like to know if this approach is good and is it possible to limit the functions and make it more generic. Alternative approach to this concept would also be helpful.

yes you can make it more generic it routes you have to add route in routes.php under config directory like this
$route['novel/(chapter-[0-9]+)/(:any)'] = 'novel/chapter/$1/$2';
now url will be www.site.com/novel/chaper-20/60
novel controller
class Novel extends CI_Controller{
function chapter($chaper_no,$page_no){
echo $chpter_no,' ',$page_no;
}
// if you don't want to pass variables to chapter function use the following
function chapter(){
$chapter_no = $this->uri->segment(2);
$page_no = $this->uri->segment(3);
}
}
if you are registering users in you site create a bookmark link ask him to save the page, if you are not registering users on your site you can set cookie to save user progress by just clicking same bookmark link or make it on each page load

Related

CodeIgniter with multiple subdomains

I have a problem and hopefully we will solve it.
The problem is, I am creating an application this application has an “Admin Panel (Backend)” and Frontend.
The URL structure of the admin panel is like this
https://www.app-name.com/admin
For frontend I have a different URL structure which is based on country for example.
https://usa.app-name.com
https://ca.app-name.com
https://uk.app-name.com
https://uae.app-name.com
https://in.app-name.com
It means in frontend I have multiple sub-domains. And the all these sub-domains are sharing a single database. As well as, backend is also sharing the same database.
Now my problem is how can I handle front-end like this.
Possible one solution in my mind is to copy app to all the sub-domains and connect with single database. And for admin panel copy it at the main domain.
But in this case, if I have a single modification to application I have to update all the copies. I want to use only single copy of app to handle all sub-domains.
Anyone have some solution for this problem. Thanks
I'd suggest executing some code in a important model for the app that gets auto-loaded through the entire app. Maybe call it Site_model.php.
Firstly, lets autoload the model. Open up config/autoload.php and add the model into the list of models to be autoloaded:
$autoload['model'] = array('Site_model');
Now, create the new model calls Site_model. In the construct of the model, add some code along these lines (this is a simply basic example of how to fetch and assign the value of a subdomain. I imagine there are better ways, but for this example, it will do):
class Site_model extends CI_Model {
var $subDomain = '';
function __construct() {
$this->subDomain = array_shift((explode('.', $_SERVER['HTTP_HOST'])));
// further code which uses the value of subDomain to fetch the correct record from the database.
}
Now, anytime you want to reference the subdomain in your code, you can simply get it like this:
echo $this->Site_model->subDomain;
This should also work for the admin backend. Oh, and ensure that the model is the first to be auto-loaded, in case any of the other models that are auto-loaded initialise code that is dependent on the site model's values.

codeigniter - routing a non existent URI

So in this codeigniter installation, I have a controller called user. In its index() I accept a user_name and show that user's profile.
class User extends CI_Controller
{
function index($user_name)
{
//show user profile...
}
}
This works fine. However to view a profile the visitor has to go to: <site>/user/john_doe
I want the URL to be <site>/john_doe. I know this is not possible using the default controller/function/arg setup that codeigniter has. Can I map this in routessuch that if a visitor types <site>/john_doe, the system recognizes there is no controller called john_doe it internally renders <site>/user/john_doe (however it should keep the original short url)
Also, there will be a side effect concern of what if a user creates a username called user which is already a valid controller...but I guess that is a secondary concern (can block all controller names as keywords so user cannot register one of them)
You can, by putting the following rule at the bottom of your routing file.
$route['/(:any)'] = "/user/index/$1";
It is of absolute importance that this rule be placed at the bottom. If you put it on top, all the URL's you enter will match the user/index controller.

Menu and all pages content from database

I'm writing simple website with some cms functions. It would be good to have all menu items and pages contents held in database.
I have table with id, name, parent_id and a content field. In future I would maybe move content to a content table to have multiple contents to menu item with fk. But it is not the case here.
The question is:
Do I need the URL field in menu table?
What else do i need to get it to work? Should every page have its own controller? I,m a beginner with zend framework, so please give me some directives. Thanks in advance.
Lets start with Question 2: every page does not need its own controller. If your pages are static you can even load every page using a single action. For more dynamic processing you could use a separate action for each page.
In any case, make sure you structure your code into controllers and actions in a way that makes sense. For example, inside your CMS a user might edit, create or delete a post. You could then create a PostController inside which you write an editAction, createAction and deleteAction.
You could store the URL in the table, but you do not necessarily have to.
Single action approach (mostly for static content)
Make sure the page id or name is stored in a GET param. You could then use the following code:
public function genericpageAction()
{
$thePageID = $this->_request->getParam('id');
// fetch the page content from the db based on $thePageID
// and pass it to the view
}
Of course, here, you could also match against the URL stored in the table if you chose that approach.
Multiple action approach (for more dynamic processing, most likely what you want with a CMS)
You could define a route for each page and load its content in the respective action. For example, for the page to edit a post:
class MyCMS_PostController extends Zend_Controller_Action
{
public function editAction()
{
// fetch the home page content
// do any further processing if necessary
}
}

Using the same model across various controllers CakePHP

I've just started reading up on CakePHP and everything is going pretty good so far, though I have a query on the best way to do the following:
I have a "User" model and a "UsersController" controller.
I currently have a "home" page which is controlled by the home_controller.php (obviously). The home page contains a registration for for a user.
The Question
When the form is posted from the home page, I need to access the User model (from the home controller).
What is the best practice for this task?
If I understand the situation correctly, I would post the form to some function in users controller. Then this function would save the data, or log in, or whatever. Finally make redirect back to home for example.
you can easily share one model across many controllers
var $uses = array('ModelName');
I do that with User Model and
Account Controller (Login, Register, ...)
Members Controller (Search, Listing, Profile, ...)
Overview Controller (Start Page, Home, ...)
for example. they all share the User Model.
I currently have a "home" page which is controlled by the home_controller.php (obviously) What other methods do you have in home_controller, other than index? And in Cake convention, the controller is plural, so: users, categories... Most likely, you are not aware of pages_controller and routing in Cake.
Anyway, yes, you can make a post to any controller (or even to another domain) from any page, just like any regular HTML page. echo $this->Form->create('User', array('controller'=>'users','action' => 'register')); You can read more here: http://book.cakephp.org/view/1384/Creating-Forms

Data from multiple models in one view (web page)

I'm new to the MVC pattern but have been trying to grasp it, for example by reading the documentation for the CakePHP framework that I want to try out. However, now I have stumbled upon a scenario that I'm not really sure how to handle.
The web site I'm working on consists of nine fixed pages, that is, there will never exist any other page than those. Each page contains something specific, like the Guest book page holds guest book notes. However, in addition, every page holds a small news box and a short fact box that an admin should be able to edit. From my point of view, those should be considered as models, e.g. NewsPost and ShortFact with belonging controls NewsPostController and ShortFactController. Notice that they are completely unrelated to each other.
Now, my question is, how do I create a single view (web page) containing the guest book notes as well as the news post box and the short fact? Do I:
Set up a unique controller GuestBookController (with an index() action) for the guest book, so that visiting www.domain.com/guest_book lets the index action fetch the latest news post and a random short fact?
Put static pages in /pages/ and in let the PagesController do the fetching?
< Please fill in the proper way here. >
Thanks in advance!
It sounds like you need to look into elements, or else you may be able to embed this into the layout - but its neater to use an element if you ask me, keep the things separate.
http://book.cakephp.org/2.0/en/views.html#elements
These allow you to have create small views that you are able to embed into other views.
You may also need to put some logic into the AppController (remember all other controllers extend the app controller) to load the data required for these views. The beforeRender function should be useful for this - its one of the hook functions cakephp provides, so if you define it on a controller, its always called after the action is finished before the view is rendered.
Something like this in your AppController should help:
function beforeRender() {
$this->dostuff();
}
function doStuff() {
// do what you need to do here - eg: load some data.
$shortfacts = $this->ShortFact->findAll();
$news = $this->NewsPost->findAll();
// news and shortfacts will be available within the $shortfacts and $news variables in the view.
$this->set('shortfacts', $shortfacts);
$this->set('news', $news);
}
If there are models you need in the app controller for use within this doStuff method, then you need to define them within uses at the top of the AppController
class AppController {
var $uses = array('NewsPost', 'ShortFact');
}

Categories