PHP - how to link other pages in view in ci - php

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

Related

How to redirect the user to another page with Codeigniter?

I'm starting with CodeIgniter and I'm having a little trouble linking the pages.
I'm doing it this way:
<li class="nav-item mr-3">
<a class="nav-link page-scroll" href="<?php echo base_url('nomeApp/login');?>">Login</a></li>
My controller is like this:
class Homepage extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('url');
public function index()
{
$this->load->view('homepage/index');
}
public function login()
{
$this->load->view('login/login');
}
}
For redirect between pages use redirect function like
redirect('controllername/method');
To navigate from navigation menu provide complete link in href
Home
If you want to rename route go to config/routes.php
$route['nomeApp/login'] = 'homepage/login';
https://www.codeigniter.com/user_guide/general/routing.html#examples
You may need to include the index.php <?php echo base_url('index.php/nomeApp/login');?>
Are you trying to send the user to a different page after a successful login?
If thats the case, you can use redirect('path/to/yourpage');
For redirect between pages use redirect function like
redirect('yourcontrollername/methodname');
If you want to rename route go to config/routes.php
$route['App/login'] = 'home/login';

redirecting page in codeigniter

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

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

Setting up a full site page in CodeIgniter

I am just starting with CodeIgniter, but I can’t quite figure how to sort out my views.
I’ve made a sample layout explaining my problem, and is attached to this post.
I have a menu box, a user-system box and a content box.
In procedural PHP I would have a page called index.php with a parameter from GET, including the content. The user-system would just be included in the index.php file inside the box, and so would the menu.
How can I do this in a proper way using MVC and CodeIgniter?
Try Most Simple Template Library for CodeIgniter. It allows you to do what you would do in your php example. Create a "main" view and channel your content to it using your controller(s). You can create "subthemes" for body, content, sidebars etc.
Hope you have configured codeigniter on your machine.
now create one file in controller folder
for example :
D:\wamp\www\demoProject\application\controllers\homePage.php
add following code in homePage controller
<?php
class HomePage extends CI_Controller {
var $controller = "homePage";
var $viewContent = array();
function list_homePage() {
// Load view pages
// Load header view page
$this->load->view('xome/header');
// Load main view page
$this->load->view('xome/list_' . $this->controller,
$this->viewContent);
// Load footer view page
$this->load->view('xome/footer');
}
}
?>
after that create one file in view folder
for example :
D:\wamp\www\demoProject\application\views\list_homePage.php
put your html code in view file.
Hope this will help you... :)
For menu I would create a model like this:
class mMenu extends CI_Model{
function mMenu(){
parent::__construct();
}
function home(){
$menu = array(
'main_menu' => '<ul>
<li>Menu Link 1</li>
<li>Menu Link 2</li>
<li>Menu Link 3</li>
</ul>'
);
return $menu;
}
}
In controller, say home:
function home(){
$this->load->model('mMenu');
$options['menu'] = $this->mMenu->home();
$this->load->view('home_view', $options);
}
In the view file, wherever I need the menu to load:
echo $menu['main_menu'];
You should do something similar to control the state of the user and return the form if the user is not logged in and anything else if the user is logged in.

Load another view when click on a image

I am using codeigniter for a project. I set controller A as my default controller in routes.php. So in this controller A it will load my home page.
If I want to access another view from the homepage for example about or contact, how do I go about doing it??
Example:
<a href="http://yoursite.com/yourcontroller/yourfunction">About Page
Now you have yourcontroller that has the yourfunction in it, from within this function you can load the view for about page, like:
function yourfunction() {
$this->load->view('your_about_view_page_name");
}
And that shall work. Hope it helps

Categories