Hello al yii users!
I am new here, and I am learnig yii, so we can propose to customers of our firm.
But first we want to test it and realizing something, just to show how it works.
Firs question: "1. Creating URLs"
yiiframework.com/doc/guide/1.1/en/topics.url
In "1. Creating URLs" guide is written:
To change the URL format, we should configure the urlManager
application component so that createUrl can automatically switch
to the new format and the application can properly understand the new URLs:"
In this First Step, I want only make the first simply change
from /index.php?r=post/read&id=100
to/index.php/post/read/id/100
In my case
from http(:2slash)vvv-mysite-com/ush/index.php?r=alf/first&prova=Athens&ok=1
to http(:2slash)vvv-mysite-com/ush/index.php/alf/first/prova/Athens/ok/1
or something similar.
It does not work.
What is wrong in my files?
Thank you in advance!
Alf_75 Milan
http(:2slash)vvv-mysite-com/ush/protected/config/main.php
// uncomment the following to define a path alias
//Yii::setPathOfAlias('local','path/to/local-folder');
// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name'=>'mysite.com',
// preloading 'log' component
'preload'=>array('log'),
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
'modules'=>array(
// uncomment the following to enable the Gii tool
/*
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'alfred',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
//'ipFilters'=>array('127.0.0.1','::1'),
),
*/
),
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
// uncomment the following to enable URLs in path-format
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'<_c:(alf)>/<id:\d+>/<_a:(first)>' => '<_c>/<_a>',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
'db'=>array(
'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=testdrive',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
*/
'errorHandler'=>array(
// use 'site/error' action to display errors
'errorAction'=>'site/error',
),
'log'=>array(
'class'=>'CLogRouter',
'routes'=>array(
array(
'class'=>'CFileLogRoute',
'levels'=>'error, warning',
),
// uncomment the following to show log messages on web pages
/*
array(
'class'=>'CWebLogRoute',
),
*/
),
),
),
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
// this is used in contact page
'adminEmail'=>'webmaster@example.com',
),
/** forse dovrebbe essere messa qua
* http://www.yiiframework.com/doc/guide/1.1/en/topics.security
* Questo dovrebbe mettere in ogni form un field hidden
* con un codice in modo da verificare che la richiesta arrivi dal nostr o sito
* Funziona.
*/
'components'=>array(
'request'=>array(
'enableCsrfValidation'=>true,
),
),
/** Invece questo dovrebbe evitare di subire degli attachi attraverso i ns cookie
* http://www.yiiframework.com/doc/guide/1.1/en/topics.security
*/
'components'=>array(
'request'=>array(
'enableCookieValidation'=>true,
),
),
);
http(:2slash)vvv-mysite-com/ush/protected/controllers/AlfController.php
class AlfController extends Controller{
public function actionFirst($prova="0",$ok="0")
{
//print Yii::app()->user->isGuest; //if user prints '1'.
/*
$session=new CHttpSession;
$session->open();
foreach($session as $name=>$value)
print "Session: $name => $value )<br />";
*/
$purifier = new CHtmlPurifier();
$purifier->options = array(
'HTML.Allowed' => 'p,a[href],b,strong,i,',
);
$model = new First;
$model->prova = (string) $purifier->purify($prova);
$model->ok = (int) $purifier->purify($ok);
/*
$model->prova = (string) $prova;
$model->ok = (int) $ok;
*/
if($model->validateAlf($model->prova,$model->ok))
$this->render('first', array(
'prova'=>$model->prova,
'ok'=>$model->ok,
));
}
}
http(:2slash)vvv-mysite-com/ush/protected/models/First.php
class First extends CFormModel
{
public $prova;
public $ok;
public function validateAlf($prova,$ok){
$this->prova = $prova;
$this->ok = $ok;
return true;
}
}
http(:2slash)vvv-mysite-com/ush/protected/views/alf/first.php
<?php
$this->pageTitle=Yii::app()->name . ' - First Test';
$this->breadcrumbs=array(
'Test',
);
?>
<h1>First</h1>
<p>This is the first file by Yii</p>
<br /><br /><br /><br /><br /><br />
<?php
print CHtml::link('Link Text',array('alf/first','prova'=>'Athens','ok'=>'1'));
//output: Link Text
//and in href there is: http(:2slash)vvv-mysite-com/ush/index.php?r=alf/first&prova=Athens&ok=1
?>
<br /><br />
<?php
$url = $this->createUrl('alf/first',array('prova'=>'Athens','ok'=>'1'));
print $url;
//output: /ush/index.php?r=alf/first&prova=Athens&ok=1
?>
<br /><br />
End of page.
http(:2slash)vvv-mysite-com/ush/.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