I'm trying to call a view in my modules/moduleName/views/ab/_temp from my index.php file. but cant seem to get it to work.
in my side controller i have this
public function actionIndex()
{
$model=new Ab;
$this->render('index',array(
'model'=>$model
));
}
in my views/site/index.php
<?php $this->renderPartial('//modulesName/views/ab/_temp', array('model'=>$model)); ?>
i'm getting this error
include(Ab.php): failed to open stream: No such file or directory
If your controller is not inside the module you should import the path to your main.php file:
'import'=>array(
...,
'application.modules.moduleName.*',
'application.modules.moduleName.models.*',
),
Related
i am new in yii framework. currently i am using yii 1.1.
now i want to create custom components and we can say than create global function which is use anywhere in the application.
According to this url 'http://www.yiiframework.com/wiki/727/updated-how-to-create-call-custom-global-function-in-whole-application/'
i am follow all steps according to above url
but i have occur a error
Alias "ext.components.MyClass" is invalid. Make sure it points to an existing PHP file and the file is readable.
MyClass.php in the components folder
class MyClass extends CApplicationComponent {
public function get_my_info() {
$value = '1';
return $value;
}
}
Declare in the config folder
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'myClass' => array(
'class' => 'ext.components.MyClass',
),
And use in the view file
<?php
$myInfo = Yii::app()->myClass->get_my_info();
echo $myInfo;
?>
Have you put the file in correct component directory?. As per your alias the path should be /protected/extensions/components/MyClass.php
Write full path like application.modules.setting.components.*
I want to include my custom js file which is in C:\xampp\htdocs\yii2\vendor\bower\backend\assets\js but console gives me error
Failed to load resource: the server responded with a status of 404 (Not Found)
While other same file have same above directory which is working.
In my appAsset file is under C:\xampp\htdocs\yii2\backend\assets
<?php
namespace backend\assets;
use yii\web\AssetBundle;
/**
* Main backend application asset bundle.
*/
class AppAsset extends AssetBundle
{
//public $basePath = '#webroot';
//public $baseUrl = '#web';
public $sourcePath = '#bower/backend/';
public $css = [
'assets/css/chosen.css',
'assets/css/style.css',
'assets/css/font-awesome.min.css',
'assets/css/bootstrap.min.css',
//'assets/css/bootstrap.css',
'assets/css/jquery.dataTables.min.css',
'assets/css/w3.css',
'assets/css/jquery-ui.css',
];
public $js = [
//'assets/js/jquery.min.js',
'assets/js/jquery-ui.js',
'assets/js/jquery.dataTables.min.js',
'assets/js/jquery-ui.multidatespicker.js',
'assets/js/chosen.jquery.js',
'assets/js/chosen.jquery.js',
'assets/js/my-custom.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
and my-custom.js
<script>
$(document).ready(function(){
$('li.active .treeview').on('click', function(e) {
$('li.active .treeview-menu').toggleClass("hide");
e.preventDefault();
});
});
</script>
The url of the file which is not finding is this
http://localhost/yii2/backend/web/assets/c4875c89/assets/js/my-custom.js
Simply add this in view file
<?php
$this->registerJsFile('PATH_TO_FILE');
?>
Use RegisterJsFile concept:
You should simply register this js file in your view, e.g. :
$this->registerJsFile('#web/js/specific.js');
Or your custompath
$this->registerJsFile('PATH_TO_FILELOCATION');
Read more :http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html#registering-scripts
and
http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerJsFile()-detail
I have placed a PDF file in folder /protected/uploads. I want to view this file on click of hyperlink. But i am facing the error and the PDF is not being displayed.
Error
Error 404<br/>
Unable to resolve the request "uploads/viewPdf".
Here is what i have done to view the file.
main.php
'urlManager'=>array(
'urlFormat'=>'path',
'rules'=>array(
'uploads/<filename:[a-zA-Z]+\.pdf>' => 'Upload/viewPdf',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
UploadController
class UploadController extends Controller
{
public function actionIndex()
{
$this->render('index');
}
public function actionViewPdf()
{
$filename = $_GET['filename'] . '.pdf';
$filepath = '/uploads/Tutorial' . $filename;
if(file_exists($filepath))
{
// Set up PDF headers
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filepath));
header('Accept-Ranges: bytes');
// Render the file
readfile($filepath);
}
else
{
// PDF doesn't exist so throw an error or something
}
}
}
and the link in the form. I would like to mention here is that the form belong to other controller not the upload controller
_form
echo CHtml::link(
'pdf',
Yii::app()->createUrl('/uploads/viewPdf', array('filename' => 'Tutorial')) ,
array('class'=>'btnPrint btn btn-danger','target'=>'_blank'));
echo CHtml::link(
'pdf',
Yii::app()->createUrl('/uploads/viewPdf', array('filename' => 'Tutorial')) ,
array('class'=>'btnPrint btn btn-danger','target'=>'_blank'));
You do NOT have a controller called uploads, you do have one called upload, this is the problem. Also I believe you should not use the first / when using create url. Try using
echo CHtml::link(
'pdf',
Yii::app()->createUrl('upload/viewPdf', array('filename' => 'Tutorial')) ,
array('class'=>'btnPrint btn btn-danger','target'=>'_blank'));
One more thing, I am not sure that the file resolves to the correct path when creating
$filepath = '/uploads/Tutorial' . $filename;
The controller is in a folder, why would /uploads/ go to the correct folder from there? Try using a full path, or detect the path with dirname(FILE) and take it from there.
Just to also make sure. You create a link that will look something like this:
/uploads/Tutorial.pdf this will get resolved to /protected/uploads/TutorialTutorial.pdf, is that the functionality you want?
Most of what Mihai said is true and should be fixed. In addition try changing your controller to include the $filename as a required parameter. This will throw a 404 if $filename is not present in the url.
public function actionViewPdf($filename){
$filename .= '.pdf';
...
}
I have a user registration form, in which I am trying to display a Captcha image by using Yii widget CCaptcha, however my image link appears broken,
Controller file:
public function actions()
{
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
);
}
Model File:
public function rules()
{
return array( array('verifyCode','captcha','allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>'insert'),
);
}
And view file:
<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as shown.
<br/>Letters are not case-sensitive.</div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>
As a answer provided somewhere I also tried giving the access rules in my controller file as
public function accessRules()
{
return array(
array('allow',
'actions' => array('captcha'),
'users' => array('*'),
),
);
}
But nothing seems to be working.
The problem was with the controller file,it should have been,
public function accessRules()
{
return array(
array('allow',
'actions'=>array('create', 'captcha'),
'users'=>array('*'),
),
}
whereas I had mentioned the action for captcha at the end, which I figured out is not allowed in Yii. All the allow actions for * should be together.
Yii captcha will create a png image. A possible explanation for the broken image link would be a missing GD extension or imagick extension, which can be identified by the presence of the following text in your error.log:
call to undefined function imagecreatetruecolor
For details and fix see "call to undefined function imagecreatetruecolor" error in PHP & pChart
How can I call a module in another module? This code works on my Controllers in Protected/Controllers:
$image = Yii::app()->image->save($photofile, 'some_name','uploadedGal');
But in the controller of my other module (admin) I get this error.
Property "CWebApplication.image" is not defined.
I have defined the image module in my main config file:
'modules'=>array(
'image'=>array(
'createOnDemand'=>true, // requires apache mod_rewrite enabled
'install'=>true, // allows you to run the installer
),
'admin',
),
You need to include the other module components in your module class. Something like this:
class AdminModule extends CWebModule
{
public function init()
{
$this->setImport(array(
'admin.models.*',
'admin.components.*',
'image.models.*',
'image.components.*',
));
}
You can import the module components in the config/main.php like below:
'import'=>array(
'application.models.*',
'application.components.*',
'application.modules.admin.models.*',
'application.modules.admin.components.*',
....
....
),
This way all models etc will be imported for you.