URI routing issue so links don't work with CodeIgniter

I am new to CodeIgniter. I have a home page with links. I know how to link the master page to another page, but I think I am doing something wrong in route.php I have looked through the tutorials but I still cannot find the problem. I've tried writing routes in different ways. Can anyone help me?

View home.php

 <ul>
          <li><a href="home.php">Homepagina</a></li>
            <li><a href="about.php">Over</a></li>
            <li><a href="<?php echo site_url('login/login'); ?>">Inloggen</a></li>
            <li><a href="register.php">Registreren</a></li>
            <li><a href="<?php echo site_url('myprofile/myuserprofile'); ?>">Profiel</a></li>
            <li><a href="matches.php">Matches</a></li>
            <li><a href="config.php">Config</a></li>
        </ul>

      

Home.php controller

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';

      

Input controller

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

      

Thanks for the help in advance!

+3


source to share


4 answers


just call controller and method

<li><a href="login/login">Inloggen</a></li>

      

If it doesn't work



<li><a href="index.php/login/login">Inloggen</a></li>

      

try it

+1


source


When you extend a class that has a constructor, you need to call that constructor from the class from which you extend it, ... ie

class Controller_name extends CI_Controller {
public function __construct(){
    parent::__construct(); // Call the CI_Controller __construct();
}

      

Note that __ is actually two underscores, but one line is displayed here.

Using your routes

routes.php

$route['default_controller'] ='pages/view';
$route['login'] = 'login/view/login';

      

Remember the CI url is of the form domainname.com/controller/method

The default_controller is where the browser will be redirected unless you supply the controller / method ie - just domainname.com. With the default controller route, you are redirected to the page controller and access the view method . Going through your code, which should be fine.

Your login must also be accurate if you access the domainname.com / login as your url - this will be sent to your login controller, view method and login pass as the page name.

home.php

 <ul>
   <li><a href="home.php">Homepagina</a></li>
   <li><a href="about.php">Over</a></li>
   <li><a href="<?php echo site_url('login/login'); ?>">Inloggen</a></li>
   <li><a href="register.php">Registreren</a></li>
   <li><a href="<?php echo site_url('myprofile/myuserprofile'); ?>">Profiel</a></li>
   <li><a href="matches.php">Matches</a></li>
   <li><a href="config.php">Config</a></li>
 </ul>

      



With your reference to home.php, you can create a controller called Home.php and define an index () method. So change

<a href="home.php">Homepagina</a>

      

to

<a href="home">Homepagina</a>

      

The link then becomes domainname.com/home - which points to your home controller and index .

Then we come to this

<a href="<?php echo site_url('login/login');

      

This will create domainname.com/login/login . Your login is only looking for domainname.com/login . So the current link domainname.com/login/login looks for a controller named login using the login method.

Your link with myprofile / myuserprofile will look for a controller called myprofile and a method called myuserprofile . There is nothing in your routes to handle this. So if that doesn't work, it has nothing to do with routes.

So this all points to you (maybe) without defining the __construct methods as I discussed at the beginning.

0


source


<li><a href="index.php/login/login">Inloggen</a></li>

      

if it works you can remove index.php. follow this documentation to remove the index page

0


source


If you don't use the full URL path, i.e. start with http[s]://

, it always concatenates smart start links with a forward slash:

<li><a href="/pages/about">Over</a></li>

      

You can use the CI URL helper and be more secure if you're struggling ™ with .htaccess and index.php with:

echo base_url('pages/about')

      

Also, while practicing static pages, take a look at the Bootable Librara docs so you can find how to pass all data to all view pages, leaving them to view files, in which case you don't need to set the second parameter 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 the whole dataset will be passed to all view files and you can make their output. Maybe you will find it useful once.;)

0


source







All Articles