redirecting page in codeigniter - php

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

Related

View file is not showing anything in a controller file in core folder

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
}
}

Where does CodeIgniter place newly created webpages?

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

PHP - how to link other pages in view in ci

I am new to CodeIgniter. I am using a bootstrap template. welcome_page.php is my view file. I have tried to link my home button to redirect to another page that is 'dashboard.php' which is in view folder of my project. I am getting an error "_the access is forbidden_ or _object is not found_".
Can anyone tell me how to link pages with an example. Thanks for the help.
controller file code:
public function home()
{
$this->load->view('dashboard');
}
view file code:
<li class="nav-item active">
<a class="nav-link" href="<?php echo BASE_PATH . "views/dashboard.php";?>">Home
<span class="sr-only">(current)</span>
</a>
</li>
The entry point for any MVC framework is the controller and you cannot access views directly.
In CodeIgniter, the url is composed of controller name and action name (public method in controller).
For example if you want the url
localhost/index.php/welcome/dashboard. You need to have a controller class called Welcome and a public method in that controller called dashboard. You can display the view by putting the command $this->load->view('welcome_message'); in function dashboard.
Url format:
<Your project root>/index.php/<small caps of the controller name>/<name of the public function in the controller>
All of this means that you just need to add function dashboard to your controller named Welcome.
Your controller should look like this.
public function home()
{
$this->load->view('dashboard');
}
public function dashboard()
{
$this->load->view('welcome_page');
}
Now you can access url localhost/index.php/welcome/dashboard
You can read more in their tutorial
https://codeigniter.com/user_guide/tutorial/static_pages.html#static-pages

Issue with URI routing so links doesn't work with CodeIgniter

I'm new to CodeIgniter. I have a homepage with links. I know how to link the homepage with another page, but I think I'm doing something wrong in the routes.php
I looked through the tutorials, but I still can't find the problem. I tried writing the routes in different ways. Can someone help me?
View of home.php
<ul>
<li>Homepagina</li>
<li>Over</li>
<li>Inloggen</li>
<li>Registreren</li>
<li>Profiel</li>
<li>Matches</li>
<li>Config</li>
</ul>
Controller of home.php
class Pages extends CI_Controller {
public function view($home ='home')
{
$this->load->helper('html');
$this->load->helper('url');
if (! file_exists(APPPATH.'views/pages/'.$home.'.php'))
{
show_404();
}
$data['title'] = ucfirst($home);
$this->load->view('templates/header',$data);
$this->load->view('templates/slideshow', $data);
$this->load->view('pages/'.$home, $data);
$this->load->view('templates/footer',$data);
}
}
routes.php
$route['default_controller'] ='pages/view';
$route['login'] = 'login/view/login';
Controller of Login
class Login extends CI_Controller {
public function view($login ='login')
{
$this->load->helper('html');
$this->load->helper('url');
if (! file_exists(APPPATH.'views/pages/'.$login.'.php'))
{
show_404();
}
$data['title'] = ucfirst($login);
$this->load->view('templates/header',$data);
$this->load->view('pages/'.$login, $data);
$this->load->view('templates/footer',$data);
}
Thank you for the help in advance!
just call controller and method
<li>Inloggen</li>
If not works
<li>Inloggen</li>
try this
When you extend a class that has a constructor, you need to call that constructor from the class you are extending it from... ie
class Controller_name extends CI_Controller {
public function __construct(){
parent::__construct(); // Call the CI_Controller __construct();
}
Note that the __ is actually two underscores but appears a a single line on here.
With your Routes
routes.php
$route['default_controller'] ='pages/view';
$route['login'] = 'login/view/login';
Remember that a CI URL is of the form domainname.com/controller/method
The default_controller is where the browser will be redirected to if you do not supply the controller/method ie - just domainname.com. With your default controller route, you will be redirecting to your pages controller and accessing the view method. Going by your code that should be fine.
Your login route should also be fine if you access domainname.com/login as your url - this will be sent to your login controller, view method and passing in login as the page name.
home.php
<ul>
<li>Homepagina</li>
<li>Over</li>
<li>Inloggen</li>
<li>Registreren</li>
<li>Profiel</li>
<li>Matches</li>
<li>Config</li>
</ul>
With your link to home.php, you might want to create a controller called Home.php and define the index() method. So change
Homepagina
to
Homepagina
The link then becomes domainname.com/home - which points to your home controller and it's index method.
Then we come to this
<a href="<?php echo site_url('login/login');
This will create domainname.com/login/login. Your login route is only looking for domainname.com/login. So the current link domainname.com/login/login is looking for a Controller called login with a method called login.
Your link with myprofile/myuserprofile will be looking for a controller called myprofile and a method called myuserprofile. There is nothing in your routes.php to deal with this. So if that is not working, it's nothing to do with the routes.
So this is all pointing back to you (possibly) not having defined the __construct methods as I discussed at the beginning.
<li>Inloggen</li>
if it works you can remove index.php.
follow this documentation to remove index page
If you don't use full path of url i.e. starting with http[s]://, it's always smart start links with fore slash:
<li>Over</li>
You can use CI url helper and be more safe in case you are struggling™ with .htaccess and index.php with:
echo base_url('pages/about')
Also, practicing static pages tutorial, pay attention on docs' Loader lybrary page, so you can find how to pass all data to all view pages, leaving it to view files and in that case you don't need to set second parameter in here:
$this->load->vars($data);
$this->load->view('templates/header');
$this->load->view('templates/slideshow');
$this->load->view('pages/'.$home);
$this->load->view('templates/footer');
And all $data array will be passed to all view files and you'll be able to make output of those.
Maybe you find it usefull once. ;)

php: Routing in CI (Codeigniter)

I really have a hard time understanding the URL/URI routing in CI. In this instance, I have two links, one is Home and the other is Panel, Home links to main/index and panel links to main/panel, heres the snippet to better understand.
Home
Panel
and this is the code for the controller main.php
class Main extends CI_Controller
{
public function index()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('templates/header');
$this->load->view('home');
$this->load->view('templates/footer');
}
public function panel()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('templates/header');
$this->load->view('panel');
$this->load->view('templates/footer');
}
}
and heres my routes (config/routes.php)
$route['main/index'] = "main/index";
$route['main/panel'] = "main/panel";
$route['default_controller'] = "main/index";
At first run, it will automatically go to main/index, it works fine, but when i click the panel link it says Object not found so does the Home link Object no found
First, you better have path relative to root in href:
Home
Panel
or, even better like this:
Home
Panel
next thing is views you are loading, correct way is to load one view in controller function:
$this->load->view('home');
and in home.php you need to include your other views, home.php:
<?php $this->load->view('templates/header');?>
...
<!--YOUR HOME HTML GOES HERE-->
...
<?php $this->load->view('templates/footer');?>
Now the routing. Be sure to use /index.php/[controller]/[function] links (unless you are using url rewrite like here http://ellislab.com/codeigniter/forums/viewthread/180566/)
Routing config:
$route['default_controller'] = "main/index"; //this is the only thing you need to define
After all your pages will be accessible via such urls:
Index page: http://example.com/ , http://example.com/index.php/main/index
Panel page: http://example.com/index.php/main/panel

Categories