Make a condition to check URL

4
Hello, I'm using CodeIgniter to develop my website, I'm trying to check which driver the user is browsing, for example, I have the login and registration area, if the user is browsing the page login I want you to show the registration button, and if it is browsing the registration page I want it to show the login button. I'm trying to do this in navbar so I do not have to create two different files just to create a simple button. Well this would be a normal way to do it:

<?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 CodeIgniter how can I proceed to do this?

    
asked by anonymous 18.02.2016 / 23:41

2 answers

4

CodeIgniter as well as many php frameworks works with Controllers and Views, in case 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');
    }
}

Signing up:

<?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 see the result of the login controller

When accessing http://localhost/ci/index.php/cadastro/ you would see the result of the controller register

In the view you will have to make the adjustment, see that you pass array( 'isLogin' => ... ) on both as true and false, so it should look 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 $islogin represents array( 'isLogin' => ... ) of the controller.

For more details see the documentation:

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

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

Documentation:

19.02.2016 / 00:26
1

you can do 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 } } ?>
    
26.02.2016 / 20:11