So I have this setup:
ARTISTAS
But when I click on that link, it takes me to http://localhost/Universal%20Music/Universal/artistas/index which says 404, like if there's nothing there but I've already created the controller with that name loading some views in it. Am I missing something here?
EDIT
My config.php has:
$config['base_url'] = 'http://localhost/Universal/Universal';
I stop using spaces in the names of the folders and now the path is:
C:\Users\Horacio\Documents\Projects\Universal\Universal
The controller file has the same name of the class I need and the code inside is:
<?php
class Artistas extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('header');
$this->load->view('main-index');
}
}
Related
I am new to CodeIgniter MVC. My home view is loaded correctly. When i replace home in index() with aboutus it is working, but when i call aboutus function then it is showing 404 file not found error. Login is also working correctly. I am not getting what is wrong. My view folder contains aboutus, home and login files.
public function index()
{
$this->load->view("home");
}
function login()
{
$this->load->view("login");
}
function aboutus()
{
$this->load->view("aboutus");
}
//Here the html code
<li>Login</li>
<li><a href="<?php echo base_url();?>index.php/aboutus" >About Us</a></li>
How to load default controller
In config/routes.php
$route['default_controller'] = "controller_name; //this load index() in provided controller
$route['default_controller'] = "controller_name/method_name"; //this load method which you created inside provided controller(ex: main/about_us)
How to create controller
Path - application/controllers/
File name - main.php
inside main.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main extends CI_Controller {
public function index()
{
$this->load->view("home");
}
function login()
{
$this->load->view("login");
}
function aboutus()
{
$this->load->view("aboutus");
}
}
How to create view
Path - `application/view/`
File name - `home.php`
File name - `login.php`
File name - `aboutus.php`
How to use Link
Home
Login
aboutus
How to use base_url()
in config/autoload.php
$autoload['helper'] = array('url');
in config/config.php
$config['base_url'] = '';
Change your html like this
home
about us
You need to add route to your new function aboutus() in routes.php
You can find routes.php in "application/config/".
suppose your controller name is 'Client_home'
Then,
eg : $route['default_controller'] = 'Client_home';
This is the reason why aboutus is working when you replace home in index() with aboutus . The index() method will be called by default, if you are not specifying which function you want to initiate.
For the aboutus() method / function, the route will be like this :
eg : $route['aboutus'] = "Client_home/aboutus";
Synatx:
$route['url'] = "controllername/method or function name"
Example for this question. Hope this help you:
http://localhost/ciexample/index.php/your_controller/your_function
i would like to know the controller name and also would like to know where u have change its configuration , if yes than which config .
use this in address bar
"localhost/folder_name_of_the_project/index/controller_name"
controller.php
<?php
define('_root',$_SERVER['DOCUMENT_ROOT']);
include(_root.'/innoshop/application/models/model.php');
// include_once 'model.php';
class Controller {
public $model;
public function __construct()
{
$this->model = new Model();
}
If i put localhost:8888/projectname, i got error like this
404 Page Not Found
The page you requested was not found.
anyone help me
As the guys say you should read the docs as this is very wrong. To fix do this..
class Controller extends CI_Controller{//better to give your controller a more meaningful name
public function __construct(){
parent::__construct();
//use the CI loader - the model will then be available like this $this->model->some_function();
$this->load->model('model');//better to give your model a more meaningful name as well
}
//the index method allows you to use just the controller name in your URI eg localhost:8888/projectname/index.php/controller
public function index(){
echo 'something to see';
}
//an alternative controller method get it like localhost:8888/projectname/index.php/controller/your_method_name
public function your_method_name(){
echo 'something to see in your method';
}
}
If you want rid of the index.php in the URI search for questions related to .htaccess in CodeIgniter
If you want to be able to use a uri like this localhost:8888/projectname then you need to add a route in config/routes.php that defines the default controller like this $route['default']='controller';
I have 3 controllers are home, food, and drink. home controller I want to make the default controller. and I wrote in routes.php file like this:
$ route ['default_controller'] = "home";
$ route ['404_override '] ='';
then i made the code in each controller as below:
controller home.php
class Home extends CI_Controller{
public function __construct() {
parent::__construct();
}
public function index(){
}
public function info(){
}
}
controller food.php
class Food extends CI_Controller{
public function __construct() {
parent::__construct();
}
public function index(){
}
public function foodMenu(){
}
public function foodJenis(){
}
}
controller drink.php
class Drink extends CI_Controller{
public function __construct() {
parent::__construct();
}
public function index(){
}
public function drinkMenu(){
}
public function drinkJenis(){
}
}
I want to make this url like www.mydomain.com/home, www.mydomain.com/food, www.mydomain.com/food/foodmenu, www.mydomain.com/food/foodjenis, www.mydomain.com/drink, www.mydomain.com/drink/drinkmenu, www.mydomain.com/drink/drinkjenis.
but when I was on the url www.mydomain.com/food/foodmenu and I headed to www.mydomain.com/food/foodjenis, but the url that appears is www.mydomain.com/food/food/foodjenis. "food" controller name in the url it into two. how to handle it. please help me. thank you :(
One way to solve is through the use of base tag.
Somewhere in the document head (before including any css or js) put
<base href='http://mysite.com'>
After that you can freely use relative links in your CI applications.
How do u make your links?
I think there is problem u try to make relative linking and html is looking at food as a folder so when u make link as
it will make link as u wrote www.mydomain.com/food/food/foodjenis
But if u use CI url
It shall work like u intended.
did you set base url in config file in CI ?If yes. try with relative paths. like food/foodjenis
I like to pass a variable to a controller trough the URL like this:
a href="<?php print base_url('/profile/' . $this->session->userdata('id')); ?>">Profiel</a>
/profile/ = controller
$this->session->userdata('id') = variable
Now I want to give it to Profile but it only works when I do /profile/index/1 and I like to it like this: /profile/1
When actually to use a new controller?
This is my controller:
<?php
class Profile extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('login');
$this->load->helper('url');
}
function index()
{
$this->load->view('profile_view');
}
}
?>
Found it,
I just need to add this line to the routes.php file in the config folder:
$route['profile/(:num)'] = "profile/index/$1"
Then when profile/1 is entered it will automatically think it's profile/index/1
Or you could use _remap function in your controller class. Read the related part in users guide. It is under "controllers" part.
Using CodeIgniter 2.0.3 on XAMPP 1.7.7 I have code where I get the error as: Unable to load the requested file: home.php
The home.php code as follows stored in ./ci2/application/controllers:
class Home extends CI_Controller {
function Home()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}
While using it just notice:
controller's name is home.php
File named home.php must be present in ci/views
Incase file extention in view folder is not php eg like its html.
then load it using : $this->load->view('home.html');
the function u need to call from outside must be public So make it:
public function index() {... }
as u are calling the constructor for the class Home. Call it like:
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home');
}
Now it will work !
Your call to $this->load->view('home'); will be looking for home.php in /ci2/application/views/. That's the file it can't find.
I'm assuming you're calling http://myapp/index.php/home/ - that means it'll automatically call the index() method.
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('home');
}
}
Please check the name of the file you have created in application/views/, because otherwise the code is correct.