I am building a web server locally using CodeIgniter and am trying to add a new page to my website. However, after creating the Controller and View files for the page I cannot access the new page at (what I think is) the desired address.
This is on a Pi running PHP 5 and Apache 2 with CodeIgniter 2.2.6. I have been able to access other pages to work through just writing the Controller and view files, but I can not access this one.
Controller page code (file name: Display_Live_State.php)
<?php
class Display_Live_State extends CI_Controller {
public function __construct()
{
//Construct page
parent::__construct();
$this->load->helper('url_helper');
}
public static function view()
{
$this->load->view('Display_Live_State_view', $data);
}
}
?>
View page code (file name: Display_Live_State_view.php)
<body>
<div>This is a test</div>
</body>
I expect to find a page with the test sentence on it at path localhost/index.php/Display_Live_State/view as I have created other pages with similar filepaths (except for the name of the controller) and have been able to access them.
If someone could enlighten me as to where CodeIgniter 'puts' the website when created, or show me what I did wrong / where the page is if it exists, I would be very grateful. Thanks.
Change Display_Live_State to Display_live_state both in the class declaration e.g. class Display_live_state and in the filename application/controllers/Display_live_state.php.
Set your base_url if you haven't already to http://localhost.
You should be able to access your controller via the url: localhost/index.php/Display_Live_State/view
Related
I have a controller file located in the core folder of my website.
I am trying to load a view file $this->load->view('nolocation');
however it's just loading a blank page, when i do the code above on any other controllers in the controller folder it shows properly.
Any ideas? Basically i want it to load a view when it cant determine the users location and not load the rest of the website.
thanks
You can check the user's location in constructor of every controller class.
Example
public function __construct(){
parent::__construct();
if(isset_location()){
redirect(base_url("nolocation")); //redirecting to the controller that specificaly designed to handle users with no location
}
}
I am fairly new to PHP MVC framework, codeigniter and in the process of developing my first website using the MVC framework. I am having trouble with my site navigation page as I want to create links that will redirect the user to the another page of the site.
Here is my controller (home_controller.php)
class Home_controller extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->view('index');
}
public function login(){
$this->load->view('login');
}
}
This is what i have in views file where i am attempting to set a link to the login page.
Staff
After running the application and attempting to navigate sign in/staff link, an error is displaying
The requested URL / home_controller/login/ was not found on this server.
I have already loaded the url helpers from autoload.php file
i have set the base_url from the config.php file to http://localhost:8888/
But its still not working for me.
Try this
<li>
Sign in ↓
<ul class="hidden">
<li>Staff</li>
</ul>
</li>
and in config.php file base_url function should be empty
Make sure If you are using CI3+ then file name should be Home_controller.php. If you using 3.0- then file name should be home_controller.php
I am new to codeigniter and really interested in trying it out. I followed the guide but ran into a problem. It seems like I am unable to load my first page properly.
I have inside the view folder another folder called general and inside it index.php.
in controller folder, I have sub-folder with general and inside it is Default controller.
I have the following route and yet the page showing is blank
$route['default_controller'] = "general/default";
$route['404_override'] = '';
When I visit the link, I type this in browser:
http://localhost:8888/treventa/
and the screen is blank. Am I doing something wrong? Sorry if this is too simple but a person got to learn from his mistake :)
Try with me step by step:
Firstly: the Controller:
(main.php) File content
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Main extends CI_Controller {
public function index() {
$this->load->view('general/welcome');
}
}
Secondly: The view:
(welcome.php) File content
You can put anything you want
<h1> Hello, I'm the view file </h1>
Finaly: The routes:
(routes.php) File content
$route['default_controller'] = "general/main";
Now call the script like this http://localhost/codeIgniter/, that's where codeIgniter is the script folder name.
I think everything now is clear.
CI trying to use method default() of general controller. If You want to use folders for controllers, make sure You specify method, so try $route['default_controller'] = "general/default/index";
And one more thing: this method (default_controller) will be used when You trying reach Your root http://localhost:8888/, if You want to route request from http://localhost:8888/treventa/ to default controller, You need to add route-rule to /config/routes.php something like $route['treventa'] = "general/main/index";
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
I am new to Silverstripe Framework / CMS. I see ./mysite/code/Page.php as controller and ./themes/simple/... as template directory. I logged into the admin panel and added new test page. The menu appears on the website with the URL http://example.com/test and content is displayed.
So what I want to know is, how to access the new controller let say Download.ss. I want to access the URL http://example.com/download/123/ without adding the new page download in admin panel. Thank you.
First of all, any files with a .ss extension are template files not controllers.
Create a new class in mysite/code/Download.php which extends Controller.
class Download extends Controller {
public function index() {
// Automatically handles URLs like http://example.com/Download
}
public function exampleaction() {
// Automatically handles URLs like http://example.com/Download/exampleaction
}
}
After that you'll want to add a new routes.yml file to the mysite/_config directory to specify that the index function on your new controller should handle calls to http://example.com/download/123.
---
Name: downloadrules
---
Director:
rules:
'download/$ID': Download
Now the '123' portion of your example URL will be accessible as $this->request->param('ID') within the index function.
Now you can do:
class Download extends Controller {
public function index() {
$fileID = $this->request->param('ID');
// Do your thing.
}
}
Documentation for this stuff is at http://doc.silverstripe.org/framework/en/reference/director