I have this code in my index.php in my view:
<p>
<?= Html::a('Create Invoice', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'inv_id',
'cust_name',
'currency',
'inv_date',
'inv_duedate',
'prod_name',
//'prod_desc',
//'prod_quanity',
'prod_price',
//'prod_tax',
//'amount',
//'subtotal',
'total',
[
'attribute' => 'image',
'format' => 'raw',
'value' => function($data){
//return Html::a($data->image, $data->image, $data->image);
return Html::a(Html::encode($data->image),$data->image);
//return Html::a($data->image, $data->image, array('target' => '_blank'));
//return Html::a(Html::encode('file'),'invoice/index');
}
],
//'poso_num',
//'subheading',
//'footer',
//'memo',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
I have already displayed the link/path of a specific file, when I click it, nothing happens. When I hover it, I can see the link, example: file:///C:/wamp3/www/basicaccounting/web/pdf/attachment.pdf, in the status bar (lower left corner of the page). I also tried right click + Open in New Tab, the url is just about:blank.
I also tried each of those commented return statements, still the same results.
Any thoughts about this?
Edit:
My problem is with my file path i.e. file:///C:/wamp3/www/basicaccounting/web/pdf/attachment.pdf
My path in the link needs to be relative to the document root i.e. /basicaccounting/web/pdf/attachment.pdf, and not in C drive.
So I tried:
'value' => function($data){
$basepath = str_replace('\\', '/', Yii::$app->basePath).'/web/';
$path = str_replace($basepath, '', $data->file);
return Html::a($data->file, $path, array('target'=>'_blank'));
}
Now it works fine.
I think I have solved my own problem.
My problem is with my file path i.e. file:///C:/wamp3/www/basicaccounting/web/pdf/attachment.pdf
My path in the link needs to be relative to the document root i.e. /basicaccounting/web/pdf/attachment.pdf, and not in C drive.
So I tried:
'value' => function($data){
$basepath = str_replace('\\', '/', Yii::$app->basePath).'/web/';
$path = str_replace($basepath, '', $data->file);
return Html::a($data->file, $path, array('target'=>'_blank'));
Now it works fine. Thanks everyone!
Related
I have a controller with an action method:
namespace frontend\controllers;
class EmployeeController extends FrontController
{
/**
* Deletes an existing Employee status.
* #param integer $id
* #return mixed
*/
public function actionDeleteStatus($status_id)
{
error_log("actionDeleteStatus " . $status_id);
return $this->redirect(['update']);
}
}
In update form, I have a detail GridView, in which I want to add a "delete" link with an URL for this method as a GET request.
I try to get the URL with this: Url::toRoute(['employee/deleteStatus','status_id' => $model->status_id]) which gives me an url like /employee/deleteStatus?status_id=4 and throws a 404, here is the detailed code:
<div class="col-xs-12">
<?php
echo Html::label(Yii::t('app', 'Employee status history'));
echo GridView::widget([
'summary' => '',
'options' => [
'id' => 'status-history',
],
'emptyText' => '',
'export' => false,
'dataProvider' => $statusHistory,
'columns' => [
[...],
[
'class' => 'kartik\grid\DataColumn',
'attribute' => 'status_id',
'headerOptions' => [ 'class' => 'kv-grid-hide' ],
'contentOptions' => [ 'class' => 'kv-grid-hide' ]
],
[
'class' => 'yii\grid\ActionColumn',
'urlCreator' => function($action, $model, $key, $index) {
return Url::toRoute(['employee/deleteStatus','status_id' => $model->status_id]);
},
'template' => '{delete}',
'contentOptions' => ['class' => 'column-action'],
'buttons' => [
'delete' => function ($url, $model, $key) {
if (Yii::$app->user->can('globalDAF')) {
$options = [
'title' => Yii::t('app', 'Delete'),
'aria-label' => Yii::t('app', 'Delete'),
'data-confirm' => Yii::t('app', 'Sure to delete status?'),
'data-method' => 'post',
'data-pjax' => '0',
'class' => 'btn-llyc'
];
return Html::a('<span class="glyphicon glyphicon-remove"></span>', $url, $options);
} else {
return;
}
}
]
]
],
'hover' => true,
'responsiveWrap' => false
]);
?>
</div>
Is the url generation wrong? Why am I getting a 404?
Thanks.
For example, index becomes actionIndex, and hello-world becomes
actionHelloWorld.
Note: The names of the action methods are case-sensitive. If you have
a method named ActionIndex, it will not be considered as an action
method, and as a result, the request for the index action will result
in an exception. Also note that action methods must be public. A
private or protected method does NOT define an inline action.
Link
Url::toRoute(['employee/delete-status','status_id' => $model->status_id])
Or in config file:
'urlManager' => [
'class' => 'yii\web\UrlManager',
#code ..
'rules' => [
'employee/deleteStatus' => 'employee/delete-status',
],
],
I want to generate URL's that can handle multiple parameters.
Eg:
www.example.com/product/index?brand=brand-name,
www.example.com/product/index?category=category-name,
I want the url be like :
www.example.com/brand-name,
www.example.com/category-name
Tried some url rules,but it doesn't work.
'rules' => [
[
'pattern' => '<brand:\w+(-\w+)*>/<category:\w+(-\w+)*>',
'route' => 'product/index',
'defaults' => [
'brand' => null,
'category' => null,
]
]
]
This is my reference :
Reference question
To do this you will have to stick to the prefixed version. So the brand param should always be prefixed with brand- and the category always with category-. Otherwise there is no way to tell what is what.
Add the following rules. This will put everything that matches brand-\w+ in the brand argument and pass it to product/index. Same for category.
'<brand:brand-\w+>' => 'product/index',
'<category:category-\w+>' => 'product/index',
To see that it works
public function actionIndex($brand = null, $category = null) {
echo "Brand: $brand<br />";
echo "Category: $category<br />";
echo Url::toRoute(['dev/index', 'brand' => 'brand-name']) . '<br />';
echo Url::toRoute(['dev/index', 'category' => 'category-name']) . '<br />';
}
I try to get POST Variables in my controller from view with:
<?= GridView::widget([
'dataProvider' => $dataProvider_products,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label'=>'Name',
'format' => 'raw',
'value'=>function ($data) use ($model, $dataProvider_products) {
return Html::a($data['name'],['suppliers_orders/addproduct', 'order' => $model->id, 'product' => $data['id'],
'data-method'=> 'post',
'data-params' => ['dataProvider' => $dataProvider_products],
]);
},
],
'supplier_product_number',
'size',
'price_net',
],
]); ?>
The parameter dataProvider will always be sent with the URL and results in a GET variable. What is wrong, respectively what must be done, that dataProvider will be sent as POST variable?
Part of my controller is:
public function actionAddproduct($order, $product){
// look for GET variable
$request = Yii::$app->request->get('data');
$dataProvider = $request['params']['dataProvider'];
// look for POST variable
$param1 = Yii::$app->request->post('dataProvider', null);
$dataProvider_suppliers_orders_products = $dataProvider;
return $this->actionView($order);
}
First of all you are passing the data-params in the url parameter rather than setting in the options parameter, so yes it will always be sent as query string no matter you pull your hairs off and become bald ¯\_(ツ)_/¯.
Then According to the DOCS
If the attribute is a data attribute as listed in
yii\helpers\Html::$dataAttributes, such as data or ng, a list of
attributes will be rendered, one for each element in the value array.
For example, 'data' => ['id' => 1, 'name' => 'yii'] generates
data-id="1" data-name="yii" and 'data' => ['params' => ['id' => 1,
'name' => 'yii'], 'status' => 'ok'] generates
data-params='{"id":1,"name":"yii"}' data-status="ok"
So, you need to change the anchor to look like
Html::a($data['name'], ['suppliers_orders/addproduct', 'order' => $model->id, 'product' => $data['id']], [
'data' => [
'method' => 'POST',
'params' => ['dataProvider' => $dataProvider_products]
]
]);
But since you are passing the $dataProvider object to the params it aint going to work because it will be changed to [Object Object] but if it is simple text then it will work, otherwise you have to change your approach.
Your complete code for the GridView should look like below
<?=
GridView::widget([
'dataProvider' => $dataProvider_products,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Name',
'format' => 'raw',
'value' => function ($data) use ($model, $dataProvider_products) {
return Html::a($data['name'], ['suppliers_orders/addproduct', 'order' => $model->id, 'product' => $data['id']], [
'data' => [
'method' => 'POST',
'params' => ['dataProvider' => $dataProvider_products]
]
]);
},
],
'supplier_product_number',
'size',
'price_net',
],
]);
?>
I am getting an error of MethodNotAllowedHttpException in compiled.php line 8931. It was working fine but suddenly it started throwing this error. I created a view in directory resources/views/main/templates/submit-for-sale.blade.php and defined its route in routes.php as:
Route::get('/submit-for-sale', function () {
return view('main.templates.submit-for-sale');
});
This view submit-for-sale.blade.php contains a form whose form tag looks like:
<form id="submit-sale-form" action="{{route('main.submit-sale-form')}}" method="post" class="form-horizontal>
{{ csrf_field() }}
.............
</form>
View is loading perfectly.
Route to post form in routes.php
Route::post('/submit-sale-form', ['as' => 'main.submit-sale-form', 'uses' => 'MainController#submitSaleForm']);
Code in MainController.php
public function submitSaleForm(Request $request)
{
$parameters = Input::get();
foreach ([
'name' => 'Name',
'email' => 'Email',
] as $key => $label) {
if (!isset($parameters[$key]) || empty($parameters[$key])) {
return response()->json(
[
'success' => false,
'error' => "{$label} cannot be empty",
]
);
}
}
}
It worked fine 2 - 3 times same code but suddenly it started throwing this error. Your help and suggestions will be highly appreciated.
Thank You.
I have the following tables/models: A, B, C, BC, D, BCD
(A:B 1:N) Connecting table D to BCD would be no problem. However I would like to filter key attributes as dropdown from A, B, C and D to find results in BCD (because in the end I need BCDid). In BCD next to BCid and Did I can store of course Aid, Bid and Cid, and it would seem to me quite an easy workaround, however I know it's totally against db normalisation. Is there another, better way (with eager loading of course)?
I've now this in BCD:
public function getB() {
return $this->hasOne(\app\models\B::className(), ['id' => 'Bid'])
->via('BC');
}
and it seems to work, but it's not eager loading.
And how do I get to model A? can I define it like this in BCD?:
public function getA() {
return $this->hasOne(\app\models\A::className(), ['id' => 'Aid'])
->via('BC')
->via(B);
}
It doesn't really work yet.
This way it works (BCSearch):
public function search($params) {
$query = BC::find()->joinWith('A', true)->joinWith('C', true);
Relation A in BC defined with "via". Dropdown filter also works.
But I still don't know how to achieve one more level deep into db structure.
This way it seems to work fully:
models/BCDSearch.php
public function search($params) {
$query = BCD::find()
->select([
'BCD.id',
'BCD.amount',
'A.id AS A_Id',
'A.name AS A_name',
'B.name AS B_name',
'B.name2 AS B_name2',
'C.name AS C_name',
'D.name AS D_name',
])
->leftJoin('BC', 'BC.id = BC_Id')
->leftJoin('B', 'B.id = B_Id')
->leftJoin('A', 'A.id = A_Id')
->leftJoin('C', 'C.id = C_Id')
->leftJoin('D', 'D.id = D_Id');
$query->andFilterWhere([
...
'A.id' => $this->A_Id,
'B.name' => $this->B_name,
'B.name2' => $this->B_name2,
'C.name' => $this->C_name,
'D.name' => $this->D_name,
]);
public function rules() {
return [
...
[[... 'A_Id', 'B_name', 'B_name2', 'C_name', 'D_name'], 'safe'],
];
}
models/base/BCD.php:
class BCD extends Whatever {
public $A_Id;
public $A_name;
public $B_name;
public $B_name2;
public $C_name;
public $D_name;
views/BCD/index.php:
GridView::widget([
'layout' => '{summary}{pager}{items}{pager}',
'dataProvider' => $dataProvider,
'pager' => [
'class' => yii\widgets\LinkPager::className(),
'firstPageLabel' => Yii::t('app', 'First'),
'lastPageLabel' => Yii::t('app', 'Last')],
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'A_Id',
'value' => 'A_name',
'filter' => yii\helpers\ArrayHelper::map(app\models\A::find()->all(), 'id', 'name')
],
[
'attribute' => 'B_name',
'value' => 'B_name',
'filter' => yii\helpers\ArrayHelper::map([['id' => 'name1', 'name' => 'name1'], ['id' => 'name2', 'name' => 'name2']], 'id', 'name')
],
[
'attribute' => 'B_name2',
'value' => 'B_name2',
'filter' => yii\helpers\ArrayHelper::map([['id' => 'name2_1', 'name' => 'name2_1'], ['id' => 'name2_2', 'name' => 'name2_2']], 'id', 'name')
],
[
'attribute' => 'C_name',
'value' => 'C_name',
'filter' => yii\helpers\ArrayHelper::map(app\models\C::find()->all(), 'C_name', 'C_name')
],
[
'attribute' => 'D_name',
'value' => 'D_name',
'filter' => yii\helpers\ArrayHelper::map(app\models\D::find()->all(), 'D_name', 'D_name')
],
'amount',
...
hope this helps others. It was not easy to figure out (at least for me) this basically easy solution. I don't know why but so far I couldn't find any relevant info on the web like this.