Mobile version of site using CodeIgniter - php

What I want to do is to create a mobile version of my web site in CodeIgniter.
I want to redirect my complete web site to m.example.com
There will be no change in controllers, neither in views and models. Both will be the same.
I don't want to change my .htaccess file. Any possible solutions for this?

The user agent class has a function;
$this->agent->is_mobile();
You could use this in the construct of your base controller(s) to test if mobile.

Instead of rewriting your code all over the place, you can just load a different view folder. In essence what will happen is you can just have CodeIgniter load a different view with the same name from another folder everytime you use $this->load->view("xxx"). First, create a new folder in your view folder called /mobile and just create views with the same exact naming conventions and it will load the the view accordingly by extending the Loader.php class.
Whether you are doing a responsive design or if you are going to create an iPhone app looking mobile version of your site, kind of like what facebook does, then you can just override the Loader class in the core folder. In your application/core folder create a MY_Loader.php and put it in that file.
Mine looks like the following
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Loader extends CI_Loader
{
//overides existing view function
function view($view, $vars = array(), $return = FALSE)
{
$CI =& get_instance();
$CI->load->library("user_agent");
if($CI->agent->is_mobile()){
$view = 'mobile/'.$view;
}
return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
}
?>
Responsive web design is a mess in my opinion, but this still separates the code for you very nicely, while still being able to use your controllers and models in unison.
Hope this helps. This is the way I will be going about it :) !

Why a redirection? If everything is the same, why not look into Responsive webdesign?
24ways.org has some good articles for it:
http://24ways.org/2012/responsive-responsive-design/
http://24ways.org/2012/responsive-images-what-we-thought-we-needed/

Ok I found another solution. I used hooks pre controller and I redirect www subdomain to m subdomain.

Related

Use function in a controller inside a folder that is defined in subfolder Controllers

Ok, guys, this is the case...
I am working in an old porject made it with CodeIgniter v2 (currently all is in localhost). For new features I created a folder called v1 inside the api folder
The structure of the project:
controllers
api
v1
visit.php
orders.php
controller1.php
controller2.php
The problem is that I can not access to the visit.php controller
to test purposes I set the visit controller in the api folder an access it whit this:
localhost/projectname/index.php/api/visit/visits
visits is the function in the visit controller
With this way everything works!! but, when I set the visit controller in the v1 folder I get a 404 page not found error.
localhost/projectname/index.php/api/v1/visit/visits
Extra
Another think that have keep in mind is. This project is using a library to the REST API so, in the visit controller are tho functions
public function visits_get(){
// return an arrays of visits
}
public function visits_post(){
// to add a new visit in a bd
}
So, the function will be called depends on the request method
I have been reading and I found that I have to configure the route.php, actually I did it but without success.
Thanks and I hope you understand what I am asking!
ROUTE.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$route['default_controller'] = "welcome";
$route['404_override'] = '';
This is all that the route.php has in it ._.
To my knowledge, any controllers falling outside the application/controllers/controller_name.php naming convention need to be explicitly defined inside the routes.php file, otherwise CI will not look inside subfolders. It's not much of a problem, actually, add something like this for your controllers:
//You'll need to do this for all of your API controllers, unfortunately
$route['api/v1/(:any)'] = 'api/v1/$1';
//If you have controllers taking arguments, eg. /api/v1/stuff/1
$route['api/v1/(:any)/(:any)'] = 'api/v1/$1/$2';
//Catch-all route for 404's, recommended
$route['api/(:any)'] = 'api/v1/error_api';
Have a look at the routing docs for CodeIgniter 2 for more info.

How to start a new website project in codeigniter?

I'm really beginner to codeigniter I'm working on CI since last 2 weeks. During this period I have created many views.php files, some controllers.php files and some models.php files
Now I want start a new website project.
What should I do. Should I delete all files of my controllers, views and models, etc., and download another codeigniter and start from the beginning?
You should check the documentation of codeigniter for help but just to give you a quick start ill explain how to create your first codeigniter project.
Installation
1 Download the codeigniter framework from http://ellislab.com/codeigniter
2 upload it in root directory of your website or local apache server directory.
Creating your codeigniter project.
In codeigniter your controller will handle the url requests and load appropriate model and views. So the first step is to create your controller.
1 Creating your controller: go to Applications->controllers and there you will find a built in controller called welcome.php.
This controller loads a view welcome_message.php which is inside Application->views.
You can use this controller or create your own.
To create your own controller create a new php file myfirstcontroller.php and extend a class with same name from CI_Controller.
Note that the name of the file and your class name should be the same. the index function is the default function that will be called when you make a request to the controller
class myfirstcontroller extends CI_Controller {
public function index(){
$this->load->view("myfirstview");
}
}
so when you request this controller through yoursite/index.php/myfirstcontroller
it will load a view called myfirstview.php which will reside inside applications->views.
Go ahead and create this file in applications ->views.
2 To pass data from controller to view you will send an array to the view
class myfirstcontroller extends CI_Controller {
public function index(){
$data['name']="My first application.";
$this->load->view("myfirstview",$data);
}
}
3 You can access this variable in view
echo $name
and it will output your variable
3 you use models you have to create a file inside applications->models and call it from controller and it will return the result in the form of array.
You can look at the documentation for further help.
Hope this helped you to get started with codeigniter.
The user guide is inside your download library.
You can also view it in http://ellislab.com/codeigniter/user-guide/
Good luck!!!
Here is Phil Sturgeon's article on how to do multiple site on one CI instance, in here he explains 2 ways of doing it and describes pros and cons.
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
But in his latest articles he has told what happened to modular separation.
http://philsturgeon.co.uk/blog/2010/03/modular-separation-codeigniter-2

showing a module anywhere in website of codeigniter

I am pretty new to codeigniter and working on an audio cart website.
I implemented a audio playlist in my project and I created a different module called playlist. Created routes and everything working fine for the page playlist. I used HMVC codeigniter and hence i have different folders for each module.
My playlist is basically a list of songs and user can select and play any song.
Modules-
---Playlist
--Controllers
--playlist.php (my front controller)
--Models
--playlistmodel.php (model)
--Views
index.php (view for showing playlist)
Now according to new specifications, This playlist can be placed anywhere in the website. It should be working. I am not able to figure out how is this feasible ? should I need to create helpers ?
Please help .
Live Url : http://webcartz.stagetesting.com/playlist
Thanks
see this url:--
How to load a module outside modules folder on HMVC with CodeIgniter?
Well you can do this too
<?php echo Modules::run('../bar/bar/index'); ?>
Perhaps you should create a library then
When we use the term "Libraries" we are normally referring to the
classes that are located in the libraries directory and described in
the Class Reference of this user guide. In this case, however, we will
instead describe how you can create your own libraries within your
application/libraries directory in order to maintain separation
between your local resources and the global framework resources.
As an added bonus, CodeIgniter permits your libraries to extend native
classes if you simply need to add some functionality to an existing
library. Or you can even replace native libraries just by placing
identically named versions in your application/libraries folder.
http://codeigniter.com/user_guide/general/creating_libraries.html
something like this
class Playlistlib {
public function __construct($params)
{
$CI =& get_instance(); // so you'd use $CI instead of $this to ref to CI object
// Do something with $params
}
public function get_playlist($params)
{
// Do something with $params
}
}
$params = array('id' => 15, 'limit' => 5);
$this->load->library('Playlistlib', $params);

Expression Engine Controllers

Im building my first site in Expression Engine, I was wondering how to use custom controllers in EE, like I would in Codeigniter, or what is the EE equivalent?
Controllers are the heart of your application, as they determine how HTTP requests should be handled.
As you're probably well-aware, a CodeIgniter Controller is simply a class file that is named in a way that can be associated with a URI.
<?php
class Blog extends CI_Controller {
public function index() {
echo 'Hello World!';
}
}
?>
The ExpressionEngine equivalent are template groups and templates, and are managed from within the Control Panel's Template Manager.
Since EE's template groups and templates can be named anything you want, the URL structure unsurprisingly loosely mimics a CodeIgniter app — after all, EE is built on CI.
For example, consider this URI: example.com/index.php/blog
CodeIgniter would attempt to find a controller named blog.php and load it.
ExpressionEngine would attempt to find the template group named blog and load the template named index.
Continuing with this example, the second segment of the URI determines which function in the controller gets called (for CodeIgniter) or which template gets loaded (for ExpressionEngine).
Building off the same URI: example.com/index.php/blog/entry
CodeIgniter would attempt to find a controller named blog.php and load it.
ExpressionEngine would attempt to find the template group named blog and load the template named entry.
Starting with the third and beyond URL segments is where CodeIgniter and ExpressionEngine start to take different approaches. (A full explanation of their differences is beyond the scope of this answer).
While there are many similarities between CodeIgniter and ExpressionEngine, at a very low-level, CodeIgniter lets you build Web Apps while ExpressionEngine lets you build Web Sites.
I know this is old, but I just thought someone looking at this might find the actual response useful.
As others have said, routes for controllers are ignored by default in ExpressionEngine.
To change this, you have to edit the first index.php and comment out the routing defaults:
// $routing[‘directory’] = ‘’;
// $routing[‘controller’] = ‘ee’;
// $routing[‘function’] = ‘index’;
Once that is done, you can add controllers just like #rjb wrote on his response.
<?php
class Blog extends CI_Controller {
public function index() {
echo 'Hello World!';
}
}
?>
After this is done, ExpressionEngine will check first for controllers and if none is found, it will look for templates.
Generally-speaking, ExpressionEngine uses template groups and templates to render content.
EE is built on CI, but it doesn't function like CI, as it's a CMS, not an application framework.

New pages in CodeIgniter on a big website

I have a website with many scripts written in "pure" PHP, i.e. no specific framework has been used to write the files. Furthermore, all the URLs are custom using .htaccess and specific PHP scripts.
For a smooth transition, I would like to start using CodeIgniter for new pages without disrupting access to the old pages, but all the documentation I've seen on CodeIgniter gives the impression that the whole website (perhaps with a few exceptions) needs to be based on the framework.
Would it be possible to use the framework for single pages here and there while leaving old URLs and code intact?
Short answer, yes.
You could access the CI framework from a subfolder, for instance, leaving the existing site untouched.
i.e
www.site.com/my_new_app/controller/method/
where my_new_app is the renamed application folder.
I'm going to go on the assumption that you already have a basic template system in place, and are able to render full pages with your existing site. Since Codeigniter is really just a framework, there's nothing to stop you from using vanilla php, like include, or additional libraries and classes. So, one thing you can do is dump your site into a sub directory in your views folder, then create a "master" controller which does nothing but load full html pages.
class Master extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
// We're expecting something like "registration/how-to-apply" here
// Whatever your URL is. The .php extension is optional
$args = func_get_args();
$path = 'path_to_my_old_site/'.explode('/', $args);
$this->load->view($path);
}
}
// Then use this in config/routes.php
$route['(:any)'] = 'master/index/$1';
This will route all pages through the master controller. So, yoursite.com/pages/faq will load the file application/views/old_site/pages/faq.php. You can apply different routes as you see fit.
This way, you can take your time migrating to use Codeigniter conventions, one page at a time.

Categories