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.
Related
I am a basic user of code igniter and PHP.I have some products page with filters. How to created a URL link like this:
http://somepage.pl/products?color=red
I know I can do above url when I will change the config line:
$config['enable_query_strings'] = FALSE; on TRUE.
But I want to use this option only one controller and one function.
Your best bet is to rely on codeigniter's native URI rewriting.
By default, (I'm not getting into custom routes here, but you might if you understand them) URLs served by Codeigniter will look like this:
BASE URL/Controller/method/params
Base url will be what you define in the config and more often than not, it'll be the base domain of your site, like example.com.
Since CI is built based on MVC architecture, all your functionality must "live" on different methods within one or many controllers. So, for example, you might have a controller named products and within that controller you may have a method (for simplicity's sake: function) called lookupProductById that will take one parameter ($product_id). It'll look like this:
class Products extends CI_Controller {
public function lookupProductById($product_id = null)
{
// whatever you need to do (like querying the database to fetch info for the product with a certain product ID) goes here
// for instance, start by checking that the product ID was passed in the URI
if ($product_id == null)
{
// handle exception
}
else
{
// query the database and fetch info for the product whose ID is $product_id
}
}
}
so, when accessing example.com/products/lookupProductById/8 you'll be able to fetch the info related to product ID 8
You may want to read the CI documentation (the introductory chapters and tutorials will guide you through a (very) basic understanding of how MVC frameworks operate, how controllers, models and views interact to produce a result, etc.) to better understand what you're getting into :)
I'm creating a Laravel 5.1 application that lists products on pages. Now each product on the database has the url of the product detail page. I have done a method that exports all those urls and convert's them to laravel routes, and the routes are written into a file and included on the laravel routing. I have done that on that way in order to be able to optimize the routing using laravel's routes:cache command. Now my question in fact is about optimization, which way would be better, to have a file with all routes, let's say 100K routes or to have a single entrance point that compares the route in question inside the database and return the respective product.
There isn't a need to have an individual route for each product. Possibly Store a unique slug(Semantic URL) in the database? Then have one route that displays a product based on a what is passed to the route.
user friendly slug could be something like 't-shirt-9000'. If you don't want this, you can always use the unique id you already have set for the product in the database.
Within your show method, you would need to query the DB with the slug past in the request to the database.
quick example:
In routes.php
Route::get('/products/{product}', 'ProductController#show');
Product Controller
public function show($product)
{
$productRequested = Product::where('url_slug', $product)->first();
// Do whatever from here
}
the fact that you use a framework you will be enforced to optimize your code
route file allow you to get the fastest way to minimize the number of mapped URLs.
the notion of route offered by Symfony to laravel allow you to manipulate all variables of your url with one single route , for example :
route::get('{category}/{id}/{slug}/{var1}/{var2}/{var3}/{varX}','yourController#yourMethod');
this route will send all variables in the url to yourMethod();
I was pushed into an existing CodeIgniter project and am familiarizing myself with the proper conventions for their MVC approach. One thing I'm stuck on is relating models and controllers. The CI documentation gives almost no guidance on this and I'm not sure what the correct approach is. Our project is an API which is called by external applications so I'm not concerned with views, only models and controllers.
This is an off-the-cuff example rather than a practical example, but hopefully it will help me understand: Let's say that I'm making an application that has minigames and a virtual store. The player can earn points by playing minigames. They can spend those points in the shop to buy items. They can also sell items in the shop to earn points. The application should log each transaction of points.
In this situation, my thinking is I would have a:
ShopModel - interacts with the database to load product data and to save what items the user has purchased
MiniGameModel - interacts with the database to load data about the minigames and save scores
PointsModel - interacts with the database to transfer points into or out of the player's account (and writes corresponding log entries in the "log" table)
ShopController - utilizes ShopModel and PointsModel
MiniGameController - utilizes MiniGameModel and PointsModel
So, for example, the user tells the application they want to search the shop for "weapons". The application requests weapons from the controller. The shop controller builds a query and the shop model executes the query and returns the records. The controller formats the records as JSON and sends them back to the application.
The user tells the application they want to buy the shield. The application notifies the shop controller. The shop controller queries the price from the shop model, then tells the points model to subtract the points, then tells the shop model to give the player the shield. Finally, the controller notifies the application via JSON that the shield was purchased.
Is this an acceptable structure, or should each controller only have one model? If there should only be one model per controller, how do I avoid copy-pasting code that handles the points? Am I doing this completely wrong?
Please do not get hung up on the database structure or anything like that. This is not a real example. I'm just trying to figure out how to organize classes.
It's really as simple as you're thinking. Models in Codeigniter are just supposed to passed db results to the controller, or custom class, then to the controller.
You can load as many models as you like in a controller - thats not a problem at all. Personally I try to keep my model > db table in a one to one relationship. If I have work to do on the data from the model, i'll usually add a Library (custom class) to handle that part.
That way my controller stays clean.
i would give you some suggestion :
To save your time use this API RESTSERVER for functionally like you
explain.
For common functionality make a library, then auto load it and when ever you need it just called it where you want.you can called a library every where ,in view,controller,model .
e.g for just an idea, Library which would look like.
<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
class Menu {
public $ci;
public $table="tblmenu";
public $full_record = array();
function __construct() {
$this->ci = &get_instance();
$this->company_id = $this->ci->session->userdata('company_id');
}
public function get_menu($comp_id = NULL) {
$comp_id = ($comp_id) ? $comp_id : $this->company_id;
$this->ci->db->select()->from($this->table)->where('company_id', $comp_id);
$this->ci->db->where('visible', 1);
$this->ci->db->order_by('id', 'ASC');
$this->full_record = $this->ci->db->get()->result_array();
return $this->full_record;
}
}
Then when you need it :
$this->load->library('menu');//or auto load it .
$this->menu->get_menu();
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
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');
}