Forbidden In Url Containing "signin" Word [Solved]

I just wrote in view:


<?php echo CHtml::link('link',Yii::app()->createUrl('signin')) ?>

But when clicking on the URL encountered with this message:

Do signin is a word key in Yii? is no problem with words like ‘singin1’ and etc.

Hi msoa

don’t use both CHtml::link and createUrl at once!

you can use either


echo CHtml::link('signin', array('signin'))

or


<a href='<?php echo Yii::app()->controller->createUrl('signin'); ?>'>signin</a>

Why have to not used CHtml::link and createUrl at once !!???

In fact i have wrote following item menu in CMenu widget:




....

array('label'=>'signin', 'url'=>Yii::app()->createUrl('signin')),

....



and in urlManager config in /config/main.php i have:




		'urlManager'=>array(

			'urlFormat'=>'path',

        	'showScriptName'=>false,

        	'caseSensitive'=>false,

        	'rules'=>array(

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

            	'index'=>'site/index',

            	'contact'=>'site/contact',

            	'register'=>'site/register',

            	'signin'=>'site/signin',

			),

		),



and in controller there is no an action named signin. when i click on signin at the menu encountered with the message:

The important thing is , there is no problem with other link names such as signin1 and . . . !!???

Hi msoa,

As KonApaz says… you can’t user CHtml::link and createUrl at once, both perform the same thing, that is:


echo CHtml::link('signin', array('signin'))

You will get a url like: <a href="http://yoursite.com/controller/signin">signin</a>

With…


<a href='<?php echo Yii::app()->controller->createUrl('signin'); ?>'>signin</a>

you will get this: <a href="http://yoursite.com/controller/signin">signin</a>

The result is the same! So in that view, instead of use this:


<?php echo CHtml::link('link',Yii::app()->createUrl('signin')) ?>

Use CHtml::link or createUrl, but not both.

Actually

echo CHtml::link(…) generates a link with appropriate url


<a href='the/generated/url'>link</a>

echo Yii::app()->controller->createUrl(‘signin’) generates only the url


the/generated/url

So, why do you want a complex code?

Thanks for guidelines. bu my main problem is access to the URL.

In fact i have wrote following item menu in CMenu widget:




....

array('label'=>'signin', 'url'=>Yii::app()->createUrl('signin')),

....



and in urlManager config in /config/main.php i have:




		'urlManager'=>array(

			'urlFormat'=>'path',

        	'showScriptName'=>false,

        	'caseSensitive'=>false,

        	'rules'=>array(

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

            	'index'=>'site/index',

            	'contact'=>'site/contact',

            	'register'=>'site/register',

            	'signin'=>'site/signin',

			),

		),



and in controller there is no an action named signin. when i click on signin at the menu encountered with the message:

The important thing is , there is no problem with other link names such as signin1 and . . . !!???

msoa,

Post your controller code, I think the problem is in your controller.

Also, did you check your code without urlManager rules?

Wthout urlManager addresses in CMenu must be like this: array(‘label’=>‘signin’, ‘url’=>array(‘signin’)), in this way signin works!

These are links in CMenu widget:




   . . . . 

   array('label'=>'signup', 'url'=>Yii::app()->createUrl('signup')),

   array('label'=>'signin', 'url'=>Yii::app()->createUrl('signin')),

   array('label'=>'signin2', 'url'=>Yii::app()->createUrl('signin2')),

	. . . . 



urlManager configuration in config/main.php




		'urlManager'=>array(

			'urlFormat'=>'path',

        	'showScriptName'=>false,

        	'caseSensitive'=>false,

        	'rules'=>array(

				'<controller:\w+>/<id:\d+>'=>'<controller>/view',

				'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

				'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

            	'index'=>'site/index',

            	'contact'=>'site/contact',

            	'signup'=>'site/signup',

            	'signin'=>'site/signin', // Only this don't work

            	'signin2'=>'site/signin2',

        	),

		),



.htaccess




RewriteEngine on

# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php

RewriteRule . index.php



siteController




<?php


class SiteController extends CController

{

	/**

     * Declares class-based actions.

     */

	public $layout = 'index';

	public function actions()

	{

		return array(

			// captcha action renders the CAPTCHA image displayed on the contact page

			'captcha'=>array(

				'class'=>'CCaptchaAction',

				'backColor'=>0xFFFFFF,

			),

			// page action renders "static" pages stored under 'protected/views/site/pages'

			// They can be accessed via: index.php?r=site/page&view=FileName

			'page'=>array(

				'class'=>'CViewAction',

			),

		);

	}


	/**

     * This is the default 'index' action that is invoked

     * when an action is not explicitly requested by users.

     */

	public function actionIndex()

	{

		// renders the view file 'protected/views/site/index.php'

		// using the default layout 'protected/views/layouts/main.php'

		$this->render('index');

	}


	/**

     * This is the action to handle external exceptions.

     */

	public function actionError()

	{

		if($error=Yii::app()->errorHandler->error)

		{

			if(Yii::app()->request->isAjaxRequest)

				echo $error['message'];

			else

				$this->render('error', $error);

		}

	}


	/**

     * Displays the contact page

     */

	public function actionContact()

	{

    	$this->layout = 'single';

		$model=new ContactForm;

		if(isset($_POST['ContactForm']))

		{

			$model->attributes=$_POST['ContactForm'];

			if($model->validate())

			{

				$name='=?UTF-8?B?'.base64_encode($model->name).'?=';

				$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';

				$headers="From: $name <{$model->email}>\r\n".

					"Reply-To: {$model->email}\r\n".

					"MIME-Version: 1.0\r\n".

					"Content-type: text/plain; charset=UTF-8";


				mail(Yii::app()->params['adminEmail'],$subject,$model->body,$headers);

				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');

				$this->refresh();

			}

		}

		$this->render('contact',array('model'=>$model));

	}


	/**

     * Displays the login page

     */

	public function actionLogin()

	{

    	$this->layout = 'single';

		$model=new LoginForm;


		// if it is ajax validation request

		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}


		// collect user input data

		if(isset($_POST['LoginForm']))

		{

			$model->attributes=$_POST['LoginForm'];

			// validate user input and redirect to the previous page if valid

			if($model->validate() && $model->login())

				$this->redirect(Yii::app()->user->returnUrl);

		}

		// display the login form

		$this->render('login',array('model'=>$model));

	}


	/**

     * Logs out the current user and redirect to homepage.

     */

	public function actionLogout()

	{

		Yii::app()->user->logout();

		$this->redirect(Yii::app()->homeUrl);

	}


	/**

 	* Registering new user

 	*/

	public function actionSignup()

	{

    	$this->layout = 'single';

    	$model = new NewUser('register');

    	if(isset($_POST['NewUser']))

    	{echo 'ccc'.$_POST['NewUser']['username'].'aaa';

        	$model->attributes = $_POST['NewUser'];

        	if($model->validate())

            	if($model->save()){

                	$url = Yii::app()->createAbsoluteUrl('verify');

                	send_verify_code($url,$model->verify_string,null,$model->email);

            	}

    	}

    	$this->render('register',array('model'=>$model));

	}


	public function actionSignin()

	{


	}

}



You’re misusing createUrl(), it needs to be like this:


Yii::app()->createUrl('site/signin')

And it will return /index.php/signin

I tried this way before, it will return /index.php/site/signin. I want returns /index.php/signin !?

Well yes, because ‘<controller>/<action>’ will be matched before ‘site/signin’ in your URL rules. Your rules need to be in this order:




                'rules'=>array(

                                'index'=>'site/index',

                                'contact'=>'site/contact',

                                'register'=>'site/register',

                                'signin'=>'site/signin',

                                '<controller:\w+>/<id:\d+>'=>'<controller>/view',

                                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

                                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

                ),



Thanks friends

Now i want user cannot access to the website/index.php/site/signin URL directly. how can i do it?

Hi @msoa

Add in the beginning of the rules


'site/signin'=>'site/signin'

...

...

Didn’t work! In this way user will be direct to website/index.php/site/signin, i want user only can obtain website/index.php/site/signin through website/index.php/signin and not accessible website/index.php/site/signin for user. Throw out a exception like 404 to user when it requests the website/index.php/site/signin.

Sorry I didn’t read your post carefully <_<

you could use this rule to redirects to home


'site/signin'=>'site/index'

or redirects to error page

'site/signin'=>'site/error'

With following rule:




'signin'=>'site/signin'

'site/signin'=>'site/error'



browser redirect to a white page without any content! I want when user requests the site/singin URL a 404 error throw out ahead the user, how can do it?

i currently use following solution:




'signin'=>'site/signin'

'site/signin'=>'signin' //signin route not exist so throw out a 404 error.



Hi @msoa again!

Add as first rule the

‘site/signin’=>‘site/nopage’

create in siteController a method like this


public function actionNopage() {

  throw new CHttpException(404,'The requested page does not exist.');

}

check also if has set accessRules() in this controller

tell us how it works.

Thanks for your votes :)

Dear @KonApaz, your posts(and other friends) are useful, so votes are minimum thing :) also Vote button is for to use ;) At last i think your last post is my answer, very thanks again.

Good luck

Thanks for support us at least with this minimum things! I know that votes are for use, also supporting each other grows and inspires the community :)

I used following code for obtain the requested action:




	public function actionNoPage() {

    	$actionPos = strrpos(Yii::app()->request->url,'/');

    	$actionId = substr(Yii::app()->request->url,$actionPos+1);

    	throw new CHttpException(404,Yii::t('yii','The system is unable to find the requested action "{action}".',array("{action}"=>$actionId)));

	}



Is there shortest way?