Not able to add a link in code igniter - php

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

Related

Yii 2.0 static link to a controller

I'm very new to Yii 2.03 (and frameworks in general). I'm facing some problem now.
On the main page I have a menu on which I want to place (in the view) the link to the controller, but I don't know how, I'm getting " PHP Fatal Error – yii\base\ErrorException
Class 'CHtml' not found" or " PHP Fatal Error – yii\base\ErrorException
Class 'Html' not found"
http://localhost/web/index.php is the main page with the menu.
http://localhost/web/index.php?r=autori/index is the page with the generated CRUD, which works perfectly.
In the view I have <li>Autori</li> and I want to replace the '#' with a valid link, I don't care for SEO now. How can I edit the view to use the specific controller?
Thanks!
If you are indeed using Yii2, then try using:
<li>Autori</li>
You'll need to include this in the beginning of your view file, so it uses the correct namespace for the Url helper class:
<?php use yii\helpers\Url ?>
But pay attention to the framework version, as Sliq noted, CHtml is a Yii1 class.

Codeigniter HMVC: Second child module not working

Please refer this question Codeigniter: Error in loading HMVC sub module model
Now I am setting up second level of module like
mysite.com/admin/hr/positions/
Directory structure is this
modules/admin/
modules/admin/models
modules/admin/controllers
modules/admin/views
modules/admin/models/dashboard/
modules/admin/controllers/dashboard/
modules/admin/views/dashboard/
modules/admin/models/hr/
modules/admin/controllers/hr/
modules/admin/views/hr/
modules/admin/models/hr/positions
modules/admin/controllers/hr/positions
modules/admin/views/hr/positions
Now I have respective MCV for every directory. Example Admin, Dashboard, HR and Positions. The system works fine till HR so if I enter site.com/admin/hr/ it is giving output
but when I try to access positions page site.com/admin/hr/positions/ it is giving me 404 no idea what is wrong in it?
Is it because that module is under HR ?
I am loading view $this->load->view('admin/hr/positions/index', $this->data); this way.
The controller name is part of the path as well.
To access function index() of class Positions in /admin/hr/positions/ you need to call /admin/hr/positions/positions/.
To avoid that, move the Controller file positions.php to /hr/.
The path structure of CodeIgniter is always /path/to/controller/controller_name/function_name/as/many/parameters/as/you/want.

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

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