How to start a new website project in codeigniter? - php

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

Related

Joomla 3 - Custom Component - How to access site model from Admin View?

I used component creator to generate a custom component for Joomla 3. I have a view in the Administrator panel that would require a function from a model in the front end.
I have been doing google searches for several days trying to locate an appropriate answer, this is the closest I have come to a working response:
How can I include multiple models in one view for in a Joomla 3.x component built with Component Creator
However, in that response he seems to be using a site view model from another site view.
Here is a little bit about my component structure:
name: com_stargazer
Admin View: email
index.php?option=com_stargazer&view=email&layout=test
/administrator/components/com_stargazer/views/email/tmpl/test.php
Site View and model: returnpage
/components/com_stargazer/models/returnpage.php
/components/com_stargazer/views/returnpage/tmpl/default.php
I tried to modify my admin view to include the site model by including the path:
$this->setModel(getModel(JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_stargazer/models', 'returnpageModel')));
$this->setModel(JModelLegacy::getInstance('returnpage', 'stargazerModel'));
// assigns array from the second model to 'ItemsOtherModel.' there is no '$' sign used.
$this->ItemsOtherModel = $this->get('tags','returnpage');
However, getModel doesn't seem like it's accessible from the view. (Probably the controller only?)
Other, references say to modify the controller (Additional references posted in comments):
https://docs.joomla.org/Using_multiple_models_in_an_MVC_component
Over the last few days, I have tried various iterations of the above referenced code samples . . . Ultimately I am confused about which controller to modify? Do I need to modify the admin controller to get this to work, or the site controller? Would it be easier to add the function to the admin model, and access it on the site view?
It's also been difficult to debug since I don't know which model is throwing the error. My best guess so far though is that I've had NO luck attaching at all to the site model from the admin view. Any help would be appreciated in getting this sorted out.
This is my first question, so I hope that it is clear enough.
I can clarify if needed.
Thanks in advance.
To call a frontend or backend Model you can use JLoader or even require_once to include the model file.
Using JLoader you can call the model inside admin view like this
JLoader::import('joomla.application.component.model'); //Load the Joomla Application Framework
JLoader::import( 'returnpage', JPATH_SITE . '/components/com_stargazer/models' ); //Call the frontend model directory
$tags_model = JModelLegacy::getInstance( 'returnpage', 'StargazerModel' );//Instantiate the model
$tags = $tags_model->gettags();
And you can also use require_once
require_once JPATH_COMPONENT_SITE.'/models/returnpage.php';
$tags_model = JModelLegacy::getInstance( 'returnpage', 'StargazerModel' );//Instantiate the model
$tags = $tags_model->gettags();

How to create 'blog' and new page in Fuel CMS with CI

I am just starting out with fuel CMS and Codeignitor. I'm looking for easy to read suggestions, references, tutorials, code snippets, ANSWERS etc for the 2 following questions below. (2-part Question)
1.) How do I access 'blog' functionality; I've read it is built in as a /view/blog.php but I don't see it; I've tried to create my own (in same directory) but it simply resolves as a static page (I created it from the dashboard) but it lacks any blog > post > get post functionality; like 'blogs' do. I've read time over, like Wordpress and Drupal; Fuel has a 'blog' template. There is none under 'layouts' as well.
So, at this point, I wouldn't mind creating my own 'blog' page - Which leads to:
2.) How do I create a new page manually in Fuel CMS, without the dashboard.
I've created an empty .php file in this directory per documentation:
C:\xampp\htdocs\FUEL-CMS-master\fuel\application\views
I don't really need a custom _variables/ with this -- so what am I missing. I've read I don't need to add / set a new controller with this type of page nor static pages. I also don't want to have to do anything with the controller if I don't need to.
Codeigniter works on CMV Controller - Model - View so to create a simple page you need to create at least 2 files 1 controller and 1 view
if you are using CI 2.2 http://www.codeigniter.com/userguide2/overview/at_a_glance.html
if you are using CI 3 http://www.codeigniter.com/user_guide/overview/at_a_glance.html
first you need to create controller
second create your view
create a file in application/controllhers/blog.php
<?php
class Blog extends CI_Controller {
public function view($page = 'home')
{
//you can acesse this http://example.com/blog/view/
}
public function new($page = 'home')
{
//you can acesse this http://example.com/blog/new/
}
}

Not able to add a link in code igniter

I am a beginner to Wamp server. I am trying to design a website with project name as "helloall" in netbeans IDE.
In the views folder I have two files layout1.php and layout2.php.
I am trying to call layout2.php from layout1.php in the below style.
<div id="logo"> <span>LAYOUT2</span> </div>
But I am facing the below error, for which I am not able to find the reason.
The requested URL /helloall/layout2.php was not found on this server.
Do I need to change anything in the configuration? I am using all the default configurations.
as per Codeigniter standard you have to follow the MVC pattern so:
Model -> Controller ->view
now, assuming you want to visualize layout2.php view you have 2 chances:
1 - load view directly where you need $this->load->view('layout2');
2 - create url function ad hoc kind of www.site.com/layout/layout1 and www.site.com/layout/layout2:
controller layout.php
class Layout extends CI_Controller {
function layout1(){
$this->load->view('layout1');
}
function layout2(){
$this->load->view('layout2');
}
}
i really suggest you to look at How To Create a Controller in Codeigniter Doc

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);

Making a controller for home page

Can you tell me how to use a controller for home page because i'm trying to put a model's data in home.ctp (homepage view) with
<?php $this->user->find() ?>but it returns
Notice (8): Undefined property:
View::$user [APP\views\pages\home.ctp,
line 1]
You should check out the cookbook; it has some solid CakePHP tutorials, at http://book.cakephp.org/
You haven't really provided alot of information, but my guess is your Controller uses a model 'User', and you're putting $this->user->find() in your view, when it should be in your controller. In your controller's action you'll want/need to do something like this...
Users_Controller extends AppController {
function index() {
$arrayOfUsers = $this->User->find(...);
$this->set('users', $arrayOfUsers);
}
}
You can then - in your View - access 'users' like so:
pre($users);
... since you used the Controller method set() to send a variable $users to the view.
All you really need to do is create a new controller if that's the direction you want to go. If this is the only statement you have that requires data access, it might be worth faking it in only this method of the PagesController. For example, one of my projects' homepages is 99% static save for a list of featured events. Rather than move everything out to a new controller or even loading my Event model for the entire PagesController (where it's not needed), I just applied this solution in PagesController::home():
$attractions = ClassRegistry::init ( 'Attraction' )->featured ( 10 );
Works great. If your page is more dynamic than mine, though, it may well be worth routing your homepage through a different controller (one that is more closely related to the data being displayed).
The default controller for the home page i.e home.ctp view, is located in /cake/libs/controller/pages_controller.php. This controller can be overriden by placing a copy in the controllers directory of your application as /app/controllers/pages_controller.php. You can then modify that controller as deem fit i.e. use models, inject variables to be used in the home page etc

Categories