Make a condition to check URL

Asked

Viewed 559 times

4

Hello, I am wearing Codeigniter to develop my website, I am trying to verify which controller the user is browsing, for example, I have the login and registration area, if the user is browsing the login page I want to show the registration button, And if he’s browsing the registration page, I want you to show him the login button. I’m trying to do this on navbar to not have to create two different files just to create a simple button. Well this would be a normal way to do:

<?php

if ($_GET['url'] === 'login') {
  echo '<li>
          <p class="navbar-btn">
            <a href="#" class="btn btn-info">
            Cadastro
            </a>
          </p>
        </li>';
}

if ($_GET['url'] === 'cadastro') {
  echo '<li>
          <p class="navbar-btn">
            <a href="#" class="btn btn-info">
            Login
            </a>
          </p>
        </li>';
}

And in the Codeigniter how can I proceed to do this?

  • What version of Codeigniter?

  • I am using version 2.1.2, I was following a tutorial on youtube, I am beginner so I downloaded the same version of the tutorial.

  • 1

    I’ll try to post an answer, but it’s CI3, but it shouldn’t change much

  • Okay, anything I switch to CI3.

  • 1

    From what I’ve looked at it looks like it’s the same on CI2 ;) can test.

2 answers

4


Codeigniter as well as many php frameworks works works with Controllers and Views, in the case of CI3 would look like this:

login:

<?php
class Login extends CI_Controller
{
    public function index()
    {
            $data['page_title'] = 'Login';
            $this->load->view('headerview', $data);
            $this->load->view('menuview', array( 'islogin' => true ));
            $this->load->view('loginview'); //isto exibe o view do login
            $this->load->view('footerview');
    }
}

Register:

<?php
class Cadastro extends CI_Controller
{
    public function index()
    {
            $data['page_title'] = 'Cadastro';
            $this->load->view('headerview', $data);
            $this->load->view('menuview', array( 'islogin' => false ));
            $this->load->view('cadastroview'); //isto exibe o view do cadastro
            $this->load->view('footerview');
    }
}

When accessing http://localhost/ci/index.php/login/ you would view the result of the controller login

When accessing http://localhost/ci/index.php/cadastro/ you would be will view the result of the registration controller

In the view menuview is that you will have to make the adjustment, see that pass the array( 'isLogin' => ... ) in both as true and false, so it should stay like this:

if ($islogin) {
  echo '<li>
          <p class="navbar-btn">
            <a href="#" class="btn btn-info">
            Cadastro
            </a>
          </p>
        </li>';
} else {
  echo '<li>
          <p class="navbar-btn">
            <a href="#" class="btn btn-info">
            Login
            </a>
          </p>
        </li>';
}

Note that the $islogin represents the array( 'isLogin' => ... ) controller’s.

For more details see the documentation:

Note that the Urls in CI3 look like this http://localhost/ci/index.php/... if you want to "remove" the index.php you can use . htaccess so:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Documentation:

  • Okay, I’ll run the tests and come back with the results

  • 1

    Thanks, it worked...

1

you can make an if using $this->Uri->segment(1), Example:

<?php if($this->uri->segment(1) == 'login'){ ?><li><p class="navbar-btn">
 <a href="#" class="btn btn-info">
  Cadastro
 </a></p></li><?php } } ?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.