PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] CakePHP Login Buttons

 February 21, 2022     cakephp, cakephp-3.0, model-view-controller, php, twitter-bootstrap     No comments   

Issue

I am new to cakephp 3.0 and trying to get my head around the whole auth components, I am trying to use a button in my template that allows me to login, However once the user is logged in, it displays logout.

This is my current HTML code

<li><a href="#"><i class="fa fa-user"></i> Login</a></li>

I have tried to call the AuthComponent::user() to check if the user is logged in but got an error, So I done some further researcha and found this way of doing it, not sure if its correct or not:

                <?php
                if($this->request->session()->read('Auth.User.id')){
                    echo $this->Html->link(
                        $this->Html->tag('i', '', array('class' => 'fa fa-lock')) . " logout",
                        array('action' => 'logout'),
                        array('escape' => false),
                        array('controller' => 'users')
                        );


                }else{
                    echo $this->Html->link(
                        $this->Html->tag('i', '', array('class' => 'fa fa-lock')) . " login",
                        array('action' => 'login'),
                        array('escape' => false),
                        array('controller' => 'users')
                        );

                }



                ?>

However I am having 2 issues.

  1. login does not change to logout once logged in.
  2. It dose not go to the correct controller to find the login or logout features (Not finding the user controller link, Tried moving it up above action, excepts this breaks the tag code

Any help with this would be appreciated.

Regards Syn


[Update - Working Code]

Hi, I have working code now, However I need to figure out how to add bootstrap icons into the buttons, Anyone know how to do this?

My working code :

            <?php
            if($this->request->session()->read('Auth.User')) {
            // user is logged in, show logout..user menu etc
                echo $this->Html->link('Logout', array('controller'=>'users', 'action'=>'logout')); 
            } else {
            // the user is not logged in
                echo $this->Html->link( 'Login', array('controller'=>'users', 'action'=>'login')); 

            }

            ?>

Solution

my bad, you was talking about Glyphicon ?

if yes :

$this->Html->link(
    '<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Login',
    array(
        'controller' => 'users',
        'action' => 'login',
        ),
    array(
        'escapeTitle' => false,
        'class' => 'btn btn-default',
        )
    );

For the bootstrap button css, just like this :

echo $this->Html->link(
    'Login', 
    array('controller'=>'users', 'action'=>'login'), 
    array('class'=>'btn btn-default');


Answered By - Blag
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing