abei1982 2017-02-07 23:22:58 5122次浏览 6条回复 2 0 0

一个好消息,阿北搞了一个github项目,会把yii2小心肝儿系列同步更新,若系列文章有误大家可以在上面提交bug,我会每天关注。 https://github.com/abei2017/small-yii2

Url

以前开发项目的时候,我们习惯于把网站网址做一个配置参数保存,善于用yii2的Url各种方法,无形中让你的代码更简单、更灵活、更规范。 下面就一一说明一下。

Url::base($scheme = false)

base函数返回当前请求的基地址,它有一个参数$scheme,默认为false,代表返回地址不包含主机信息。 $scheme参数说明

  • false 返回地址不包含主机信息
  • true 包含主机地址
  • http 返回http协议的主机地址
  • https 返回https协议的主机地址
  • '' 返回相对的主机地址

我们以http://yii2-study.local.com/index.php?r=site/index 来举例

    Url::base();
    //  返回为''
    Url::base(true)
    //  返回为http://yii2-study.local.com
    //  记住:最后没有反斜杠哦
    Url::base('http');
    //  返回为http://yii2-study.local.com
    Url::base('https');
    //  返回为https://yii2-study.local.com
    Url::base('');
    //  返回为//yii2-study.local.com

切记:http https ''这些参数,函数base并没有去判断,只是简单的字符串替换,如果你输入了base('d'),则返回 d://yii2-study.local.com

Url::canonical()

返回当前请求的标准url 我们还是以http://yii2-study.local.com/index.php?r=site/index 来举例

    echo Url::canonical();
    //  返回结果为 http://yii2-study.local.com/index.php?r=site/index

Url::current(array $params = [], $scheme = false)

返回当前请求+GET参数,重点是该函数配合参数还能增加删除GET参数,这在我们某些url匹配时候会变得非常有用。 我们以http://yii2-study.local.com/index.php?r=site/index&id=78&cat=me 来举例

    echo Url::current();

执行上述代码后我们会得到 /index.php?r=site/index&id=78&cat=me 这样的结果

请注意,current()和canonical()区别,如果对本例里的url执行canonical()函数,会得到http://yii2-study.local.com/index.php?r=site/index ,相比较current,它多了主机信息,少了GET参数(路由请求除外)。

上面是current获取当前的Url,我们还可以使用current对请求进行修改和删除等操作。

例子1:我们对当前请求增加一个name=abei GET参数,只需要执行

    echo Url::current(['name'=>'abei']);

于是我们获得了预想的结果/index.php?r=site/index&id=78&cat=me&name=abei

例子2:我们要删除例子url中的cat参数,只需要执行

    echo Url::current(['cat'=>null]);

则结果为/index.php?r=site/index&id=78 哈哈,cat参数被删除了

千万记住:删除一个GET参数的时候,只能设置current对应参数值为null,设置成false或''都是没用的。

例子3:更新例子中url的cat参数为you

    echo Url::current(['cat'=>'you']);

是的,yii2很贴心的将将结果返回为 /index.php?r=site/index&id=78&cat=you。感谢薛强,虽然现在在yii2的github已经看不到你的comment,仍然要感谢你创造了yii2.

一个大问题,current返回的结果中如何包含主机信息那? 你只需,只需将current的第二个参数设置为true就ok了。

Url::to($url = '', $scheme = false) 和 Url::toRoute($route, $scheme = false)

生成一个URL,to()和toRoute()只有一个不同,那就是当to()的第一个函数为一个字符串的时候,会直接返回,而toRoute会将其解析成controller/action或action,然后返回url。 下面我们来举几个例子来说明下

    Url::to(['site/about','cat'=>'abei']);
    Url::toRoute(['site/about','cat'=>'abei']);
    //  以上两个函数输出了同一个结果 index.php?r=site/about&cat=abei
    Url::to('site/index');
    Url::toRoute('site/index');
    //  以上两个函数输出结果不同 to()的结果为site/index toRoute()的结果为index.php?r=site/index
    Url::to();
    Url::toRoute();
    //  to()无参数时返回了当前的路由+GET,而toRoute报错了(toRoute第一个参数不允许不存在)
    Url::to("@web/images/logo.gif");
    Url::toRoute("@web/images/logo.gif");
    //  to和toRoute均支持别名。
    Url::to(['site/index','#'=>'name');
    Url::toRoute(['site/index','#'=>'name')
    //  这里有一个特殊的#,使用他能实现内部锚点,to和toRoute均可以生成index.php?r=site/index#name

当然,to和toRoute也有第二个参数,决定返回的url是否含有主机信息。

哥俩好~ Url::previous($name = null)和Url::remember($url = '', $name = null)

你是否有过这样的需求,比如记录一个会员最近30个访问的路径,使用previous和remember就能轻易实现,就和他们的名字一样,用remember可以存储当前路径,使用previous可以把remember存起来的url展示出来。 来来来,贴代码,其他都没有用。

    Url::remember('http://www.a.com','a');
    Url::remember('http://www.b.com','b');
    var_dump(Url::previous('a'));
    //  结果输出了http://www.a.com

yii2记住了,小提示,是session原理。

我想细心的你一定发现了,是的,previous和remember的参数是可以为空的,没错,阿北用代码来说明。 我们还是以 http://yii2-study.local.com/index.php?r=site/index为例

    echo Url::previous();
    //  当previous参数为空时,返回returnUrl。
    echo Url::remember();
    //  对于remember函数,当第一个参数$url不提供,将记住当前路由请求(即http://yii2-study.local.com/index.php?r=site/index),第二个参数$name不提供,则默认为yii\web\User::$returnUrlParam

其他!

当然Url还有一些其他函数,比如home()等,比较简单,就不一一说明,主要上面几个大方法掌握了,Url基本就过了。

  • 回复于 2017-02-27 17:09 举报

    marked

  • 回复于 2017-06-30 11:41 举报

    public function actionShow(){

    $joke=Joke::find()->with('comment')->where(['state'=>1]);
    $count=$joke->count();
    $pagination = new Pagination(['totalCount' => $count,'pageSize'=>2]);
    $data = $joke->offset($pagination->offset)
        ->limit($pagination->limit)
        ->all();
    $form=new Form();
    $type=Types::find()->asArray()->all();
    $type=$form->getSel($type,'type_name','type_name');
    return $this->render("show",['pagination'=>$pagination,"data"=>$data,"type"=>$type,"model"=>$form]);
    

    }

  • 回复于 2017-06-30 11:41 举报

    public function actionNum()
    {

    $joke=Joke::find()->with('comment');
    $count=$joke->count();
    $pagination = new Pagination(['totalCount' => $count,'pageSize'=>2]);
    $data = $joke->offset($pagination->offset)
        ->limit($pagination->limit)
        ->all();
    //print_r($data);die;
    $form=new Form();
    $type=Types::find()->asArray()->all();
    $type=$form->getSel($type,'type_name','type_name');
    return $this->render("num",['pagination'=>$pagination,"data"=>$data,"type"=>$type,"model"=>$form]);
    

    }

  • 回复于 2017-06-30 11:42 举报

    public function actionLists()
    {

    $form=new Form();
    $id = yii::$app->request->get('id');
    $arr = Joke::find()->with('comment')->where('joke_id='.$id)->asArray()->one();
    //$type=Types::find()->asArray()->all();
    $type=Types::find()->asArray()->all();
    $type=$form->getSel($type,'type_name','type_name');
    //print_r($arr);die;
    return $this->render("lists",['arr'=>$arr,'model'=>$form,'type'=>$type,'id'=>$id]);
    

    }

  • 回复于 2017-06-30 11:42 举报

    public function actionUpt()
    {

    $arr = yii::$app->request->post('Form');
    $state = $arr['state'];
    if($state == 1)
    {
        //print_r($arr);die;
        $joke_type = implode(',',$arr['type_id']);
        //print_r($arr);die;
        $sql = "update joke set joke_type='".$joke_type."',state='".$arr['state']."' where joke_id=".$arr['joke_id'];
        //print_r($sql);die;
        $info = yii::$app->db->createCommand($sql)->execute();
        //print_r($info);die;
        $sql1="select comment_id from joke where joke_id=".$arr['joke_id'];
        $res = yii::$app->db->createCommand($sql1)->queryOne();
        $sql2 = "update comment set comment='".$arr['desc']."' where comment_id=".$res['comment_id'];
        $re = yii::$app->db->createCommand($sql2)->execute();
        return $this->redirect('?r=joke/show');
    }else
    {
        return $this->redirect('?r=joke/num');
    }
    

    }

  • 回复于 2017-06-30 11:42 举报

    <?php
    use yii\helpers\Html;
    use yii\widgets\ActiveForm;

    $form = ActiveForm::begin([

    'id' => 'login-form',
    'options' => ['class' => 'form-horizontal'],
    'action'=>'?r=joke/upt',
    'method'=>'post',
    

    ]) ?>
    <?php $model->username=$arr['joke_author'] ?>

    <?= $form->field($model, 'username')->textInput() ?>
    <?php $model->type_id=explode(',',$arr['joke_type']) ?>
    <?php echo $form->field($model, 'type_id')->checkboxList($type) ?>
    <?php $model->desc=$arr['comment']['comment'] ?>
    <?= $form->field($model, 'desc')->widget('common\widgets\ueditor\Ueditor',[
    
        'options'=>[
            'initialFrameWidth' => 850,
        ]
    
    ]) ?>
    

    <?php $model->state=$arr['state'] ?>
    <?= $form->field($model, 'state')->radioList(['0'=>'不通过','1'=>'通过']) ?>

    <?= $form->field($model,'joke_id')->hiddenInput(['value'=>$id])?>
    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('修改', ['class' => 'btn btn-primary']) ?>
        </div>
    </div>
    

    <?php ActiveForm::end() ?>

您需要登录后才可以回复。登录 | 立即注册