1

I'm trying to include a php file in yii framework but is not working, that's my code:

$url = Yii::app()->request->baseUrl.'/css/new/x/x/x.php';
include $url;

I tried to use require and require_once but it gives error.

2
  • Whats the error? And whats the value of $url Commented Oct 29, 2015 at 18:41
  • 1
    you can use Yii::import('pathalias'.x.php) Commented Nov 2, 2015 at 5:30

3 Answers 3

3

Try this:

$url = Yii::app()->basePath.'/../css/new/x/x/x.php';
include $url;

And for a more comprehensive analysis have a look at this: Yii file include does not work, while i can access it by typing in URL

Sign up to request clarification or add additional context in comments.

Comments

1

First, why the PHP file is located inside the CSS folder?

If it is a helper, it could be located at protected/helpers folder. If it is a extension, it could be located at protected/extensions, and so on. I sugest you read http://www.yiiframework.com/wiki/155/the-directory-structure-of-the-yii-project-site/.

Once the file is in the correct place, you can use Yii::import() or Yii::getPathOfAlias() to include the file. This way you use alias instead of paths:

1.Define an alias. It could be done inside your entry script (index.php file):

define('COMMON_PATH', dirname(__FILE__).'/common');
Yii::setPathOfAlias('common', COMMON_PATH);

2.Import or include the file. If included file represent a class you can use Yii::import("common.path.to.your.file.inside.common.folder.YourFile"); If included file has code to be executed use Yii::getPathOfAlias() and include():

$folder = Yii::getPathOfAlias('common.path.to.your.file.inside.common.folder');
include($folder.'/file.php');

1 Comment

The include statement includes and evaluates the specified file. Yii::import only imports a class or a directory.
1

In Yii2 replace app() with $app

$url = Yii::$app->basePath.'/../css/new/x/x/x.php';
include $url;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.