Best Way To Use Isguest?

Hello,

I am in Yii2 trying to add one link called "Posts" only accessible to logged in users.

I cannot work out how to use the IsGuest, I tried this but it does not work:




 echo Nav::widget([

                'options' => ['class' => 'navbar-nav navbar-right'],

                'items' => [

                    ['label' => 'Home', 'url' => ['/site/index']],

                    ['label' => 'About', 'url' => ['/site/about']],

                    ['label' => 'Contact', 'url' => ['/site/contact']],

                    

                        Yii::$app->user->isGuest ? ['label' => 'Login', 'url' => ['/site/login']] :

                        ['label' => 'Logout (' . Yii::$app->user->identity->username . ')',

                            'url' => ['/site/logout'],

                            'linkOptions' => ['data-method' => 'post']],

                        Yii::$app->user->isGuest['label' => 'Posts', 'url' => ['/posts']],   

                ],

            ]);

            NavBar::end();




Any idea why please?

Thank you,

Ben

you can use isGuest for the menu visibility like




'visible'=>!Yii::$app->user->isGuest



on the navbar remember the isGuest return boolean values.

Thank you so much Ahamed, I understand now. It worked!

I have also understood that this is only for the visual part because I can still access the posts url when typed manually. I will have to check out and understand how permissions work.

In the mean time I am having a really annoying problem there, no idea if you or anyone could help me please.

In another post I explained that I was following the tutorial which said to add the below code (in my posts model if I understood correctly) so I could get the creation_time and update_time field in the database filled up automatically. The problem is that I am getting an error when I add the below code in my Posts model file.

Any idea why please? Thanks!




use yii\db\Expression;


public function behaviors()

{

    return [

           'timestamp' => [

            'class' => TimestampBehavior::className(),

            'attributes' => [

                ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time',

                ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',

            ],

            'value' => new Expression('NOW()'),

        ],

    ];



####################ERROR#######################

PHP Fatal Error – yii\base\ErrorException

Trait ‘Yii\db\Expression’ not found

####################ERROR#######################

I went in the Expression.php file and could not find the class TimestampBehaviour, Iam using the basic version, is it why?

This is what I have inside that file:




<?php

/**

 * @link http://www.yiiframework.com/

 * @copyright Copyright (c) 2008 Yii Software LLC

 * @license http://www.yiiframework.com/license/

 */


namespace yii\db;


/**

 * Expression represents a DB expression that does not need escaping or quoting.

 * When an Expression object is embedded within a SQL statement or fragment,

 * it will be replaced with the [[expression]] property value without any

 * DB escaping or quoting. For example,

 *

 * ~~~

 * $expression = new Expression('NOW()');

 * $sql = 'SELECT ' . $expression;  // SELECT NOW()

 * ~~~

 *

 * An expression can also be bound with parameters specified via [[params]].

 *

 * @author Qiang Xue <qiang.xue@gmail.com>

 * @since 2.0

 */

class Expression extends \yii\base\Object

{

    /**

     * @var string the DB expression

     */

    public $expression;

    /**

     * @var array list of parameters that should be bound for this expression.

     * The keys are placeholders appearing in [[expression]] and the values

     * are the corresponding parameter values.

     */

    public $params = [];


    /**

     * Constructor.

     * @param string $expression the DB expression

     * @param array $params parameters

     * @param array $config name-value pairs that will be used to initialize the object properties

     */

    public function __construct($expression, $params = [], $config = [])

    {

        $this->expression = $expression;

        $this->params = $params;

        parent::__construct($config);

    }


    /**

     * String magic method

     * @return string the DB expression

     */

    public function __toString()

    {

        return $this->expression;

    }

}




Thanks

Ben

"use" has to be added outside of a class, directly after the namespace statement in your file.

Thanks Cebe