[英]Running a module controller in yii
我剛剛在myfirstmodule
使用gii創建了一個名為myfirstmodule
的模塊,然后在瀏覽器中按如下所示的URl :
本地主機/yii_learn/index.php?r=myfirstmodule
defaultcontroller
將運行並顯示輸出。 現在,我創建了一個新的控制器,並在同一模塊中進行查看,並通過以下方式運行:
http://localhost/yii_learn/index.php?r = myfirstmodule / mycontroller / index
它將我重定向到項目的主頁。
下面是代碼:
mycontroller.php
class mycontroller {
//put your code here
public function actionIndex(){
$this->render('myfirst');
}
我的查看文件代碼是
<?php
$this->breadcrumbs=array(
$this->module->id,
);
?>
<h1><?php echo $this->uniqueId . '/' . $this->action->id; ?></h1>
<p>
This is the view content for action "<?php echo $this->action->id; ?>".
The action belongs to the controller "<?php echo get_class($this); ?>"
in the "<?php echo $this->module->id; ?>" module.
</p>
<p>
You may customize this page by editing <tt><?php echo __FILE__; ?></tt>
</p>
Main.php文件代碼
<?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'=>'My Web Application',
// preloading 'log' component
'preload'=>array('log'),
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),
'modules'=>array('testmodule','CustomerOnBoarding',
'myfirstmodule'=>array(),
// 'myfirstmodule'=>array(
// 'class'=>'\myfirstmodule\DefaultController',
// ),
// uncomment the following to enable the Gii tool
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>false,
// 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(
// 'caseSensitive' => true,
// 'urlSuffix' => '/',
// 'showScriptName' => false,
//
// 'urlFormat'=>'path',
'rules'=>array('myfirstmodule'=>'myfirstmodule/mycontroller/index',
// '<controller:\w+>/<id:\d+>'=>'<controller>/view','<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
// database settings are configured in database.php
'db'=>require(dirname(__FILE__).'/database.php'),
'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',
),
);
誰能在這方面幫助我,那我該如何運行控制器並查看。
網址格式為http://localhost/yii_learn/index.php?r=controllername/functionname
。 如果要通過url訪問功能,則應在其前面加上action
。 例如,一個功能actionSampleFunction()
中testcontroller
可接http://localhost/yii_learn/index.php?r=testcontroller/samplefunction
。
您是否嘗試通過訪問規則來執行此操作? 只需將其添加到您的控制器並嘗試。
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index'),
'users'=>array('*'),
),
);
}
如果工作了,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.