在yii中运行模块控制器

时间:2015-09-17 05:10:51

标签: php yii

我刚刚在yii中使用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',
),
 );

任何人都可以帮助我,我怎样才能运行我的控制器并查看。

2 个答案:

答案 0 :(得分:0)

网址格式为http://localhost/yii_learn/index.php?r=controllername/functionname。如果要通过url访问函数,则应使用action作为前缀。例如,actionSampleFunction()可以访问testcontroller中的http://localhost/yii_learn/index.php?r=testcontroller/samplefunction函数。

答案 1 :(得分:0)

您是否尝试过此操作的访问规则 喜欢。只需将其添加到您的控制器并尝试。

public function accessRules()
{
    return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
            'actions'=>array('index'),
            'users'=>array('*'),
        ),
    );
}

如果工作让我知道。