How to set link correctly in anonymous finction

Hi guys, I code follwong link in anyonymous function,which should transfer to another CRUD in another folder with another Controller. What I actually get will be shown up in attachement_0. As U can see there,this GridView is a mixture of both tables. I want to get content of just one table,as u can see in attachement_1

Here is my code of anonymous function:





    [

        'class' => 'yii\grid\ActionColumn',

        'template' => '{anlagen_index} <br>{anlagen_create} ',

        'buttons' => [

            'anlagen_index' => function ($url,$id) {

            $url=\Yii::$app->urlManager->baseUrl . '/index.php/dateianhang';

                return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', $url, ['title' => 'Anlagen anzeigen']);

            },

            'anlagen_create' => function ($url, $id) {

                $url="dateianhang_create";

                return"<br>" . Html::a('<span class="glyphicon glyphicon-paperclip"><span class="glyphicon glyphicon-plus"></span></span>', $url, ['title' => 'Anlagen hinzufügen']);

            },

        ],

    ],



Any ideas,how to set link correctly in GridView?

Here is code of GridView,which will be rendered as having shown up in attachement_0





<?php


/* @var $this yii\web\View */

/* @var $searchModel frontend\modules\dateianhang\models\DateianhangSearch */

/* @var $dataProvider yii\data\ActiveDataProvider */


use yii\helpers\Html;

use yii\widgets\Pjax;

use kartik\export\ExportMenu;

use kartik\grid\GridView;


$this->title = Yii::t('app', 'Dateianhang');

$this->params['breadcrumbs'][] = $this->title;

$search = "$('.search-button').click(function(){

$('.search-form').toggle(1000);

return false;

});";

$this->registerJs($search);

?>




<h1><?= Html::encode($this->title) ?></h1>

        <?php // echo $this->render('_search', ['model' => $searchModel]); ?>


<p>

    <?= Html::a(Yii::t('app', 'Create Dateianhang'), ['create'], ['class' => 'btn btn-success']) ?>

            <?= Html::a(Yii::t('app', 'Advance Search'), '#', ['class' => 'btn btn-info search-button']) ?>

</p>

        <div class="search-form" style="display:none">

    <?=  $this->render('_search', ['model' => $searchModel]); ?>

    </div>

.

.

.




[

        'buttons' => [

            'anlagen_index' => function ($url, $id) {

                return Html::a('', ['/url/to/any/point'], ['title' => 'Anlagen anzeigen']);

            },

        ],

    ],

This won’t fix my problem. If I will try as U suggested(defining url as an array). I will create following url:




http://localhost/yii2_perswitch/frontend/web/index.php/yii2_perswitch/frontend/web/index.php/dateianhang



The correct url(I use prettUrl=>true in config) is like this




 $url=\Yii::$app->urlManager->baseUrl . '/index.php/dateianhang';



In Browser like this:




http://localhost/index.php/dateianhang



if I will code like this:




 return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', ['dateianhang'], ['title' => 'Anlagen anzeigen']);



I will get following url, which is also incorrect:




http://localhost/yii2_perswitch/frontend/web/yiic.php/bewerber/bewerber/dateianhang



P.S.: I defined rule in config like this:




  'dateianhang' => 'dateianhang/dateianhang/index',



My actual script is defined like this:




'bewerber' => 'bewerber/bewerber/index',



The only way to get a valid url is definig it as string, but this will render both models as explained above Problem is not link,'cause after loading page again,pushing return in browser url strip, script will be loaded propably. Return value won’t load script propably(look at my thumbnails attached[size=“3”]…[/size])





    [

        'class' => 'yii\grid\ActionColumn',

        'template' => '{anlagen_index} <br>{anlagen_create} ',

        'buttons' => [

            'anlagen_index' => function ($url,$id) { //this won't be loaded propably!!

                return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', 'dateianhang', ['title' => 'Anlagen anzeigen']);

            },

            'anlagen_create' => function ($url, $id) {  //this will be loaded propably!!

                $url="dateianhang_create";

                return"<br>" . Html::a('<span class="glyphicon glyphicon-paperclip"><span class="glyphicon glyphicon-plus"></span></span>', $url, ['title' => 'Anlagen hinzufügen']);

            },

        ],

    ],



I may be wrong, but you seem to have got the url manager config completely messed up. :-[ :-[ :-[

Would you please show us your config for ‘urlManager’?




'components' => [

    ...

    'urlManager' => [

        'enablePrettyUrl' => true,

        'rules' => [

            ... what rules do you have ? ...

        ],

        'showScriptName' => false,  // do you have this line?

    ],

    ...



I have a lot of rules,in frontend as well as in backend. My link problem has nothing to do with rules, 'cause I principally can see all sites necessary. Here is my config:





<?php


$params = array_merge(

        require(__DIR__ . '/../../common/config/params.php'), require(__DIR__ . '/../../common/config/params-local.php'), require(__DIR__ . '/params.php'), require(__DIR__ . '/params-local.php')

);


return [

    'id' => 'app-frontend',

    'basePath' => dirname(__DIR__),

    'bootstrap' => ['log'],

    'controllerNamespace' => 'frontend\controllers',

    'modules' => [

        'bewerber' => [

            'class' => 'frontend\modules\bewerber\Bewerber'

        ],

        'bewerbung' => [

            'class' => 'frontend\modules\bewerbung\Bewerbung'

        ],

        'kontakt' => [

            'class' => 'frontend\modules\kontakt\Kontakt'

        ],

        'kunde' => [

            'class' => 'frontend\modules\kunde\Kunde'

        ],

        'mail' => [

            'class' => 'frontend\modules\mail\Mail'

        ],

        'mitarbeiter' => [

            'class' => 'frontend\modules\mitarbeiter\Mitarbeiter'

        ],

        'newsletter' => [

            'class' => 'frontend\modules\newsletter\Newsletter'

        ],

        'vakanz' => [

            'class' => 'frontend\modules\vakanz\Vakanz'

        ],

        'dateianhang' => [

            'class' => 'frontend\modules\dateianhang\Dateianhang'

        ],

    ],

    'components' => [

        'i18n' => [

            'translations' => [

                '*' => [

                    'class' => 'yii\i18n\PhpMessageSource',

                    'basePath' => '@frontend/messages', // if advanced application, set @frontend/messages

                    'sourceLanguage' => 'de',

                ],

            ],

        ],

        'request' => [

            'csrfParam' => '_csrf-frontend',

        ],

        'user' => [

            'identityClass' => 'common\models\User',

            'enableAutoLogin' => true,

            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],

        ],

        'session' => [

            // this is the name of the session cookie used for login on the frontend

            'name' => 'advanced-frontend',

        ],

        'log' => [

            'traceLevel' => YII_DEBUG ? 3 : 0,

            'targets' => [

                [

                    'class' => 'yii\log\FileTarget',

                    'levels' => ['error', 'warning'],

                ],

            ],

        ],

        'errorHandler' => [

            'errorAction' => 'site/error',

        ],

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => true,

            'enableStrictParsing' => true,

            'class' => 'yii\web\UrlManager',

            'rules' => [

                '/' => 'site/login',

                'home' => 'site/index',

                'reset' => 'site/request-password-reset',

                'about' => 'site/about',

                'contact' => 'site/contact',

                'logout' => 'site/logout',

                'signup' => 'site/signup',

                'demo' => 'site/record',

                'bewerber' => 'bewerber/bewerber/index',

                'bewerber_create' => 'bewerber/bewerber/create',

                'bewerber_view' => 'bewerber/bewerber/view',

                'bewerber_save' => 'bewerber/bewerber/save-as-new',

                'bewerber_update' => 'bewerber/bewerber/update',

                'bewerber_pdf' => 'bewerber/bewerber/pdf',

                'bewerber_delete' => 'bewerber/bewerber/delete',

                'picture_delete' => 'bewerber/bewerber/delete_picture',

                'adresse' => 'kontakt/kontakt-adresse/index',

                'adresse_create' => 'kontakt/kontakt-adresse/create',

                'adresse_view' => 'kontakt/kontakt-adresse/view',

                'adresse_save' => 'kontakt/kontakt-adresse/save-as-new',

                'adresse_update' => 'kontakt/kontakt-adresse/update',

                'adresse_pdf' => 'kontakt/kontakt-adresse/pdf',

                'adresse_delete' => 'kontakt/kontakt-adresse/delete',

                'telefon_create' => 'kontakt/kontakt-adresse/create_telefon',

                'telefon_view' => 'kontakt/kontakt-adresse/view_telefon',

                'telefon_save' => 'kontakt/kontakt-adresse/save-as-new_telefon',

                'telefon_update' => 'kontakt/kontakt-adresse/update_telefon',

                'telefon_pdf' => 'kontakt/kontakt-adresse/pdf_telefon',

                'telefon_delete' => 'kontakt/kontakt-adresse/delete_telefon',

                //'dateianhang_default' => 'dateianhang/default/index',

                'dateianhang' => 'dateianhang/dateianhang/index',

                'dateianhang_create' => 'dateianhang/dateianhang/create',

                'dateianhang_update' => 'dateianhang/dateianhang/update',

                'dateianhang_view' => 'dateianhang/dateianhang/view',

                'dateianhang_save' => 'dateianhang/dateianhang/save-as-new',

                'dateianhang_delete' => 'dateianhang/dateianhang/delete',

                'dateianhang_pdf' => 'dateianhang/dateianhang/pdf',

                'pdf' => 'gridview/export/download',

                '<controller:\w+>/<id:\d+>' => '<controller>/view',

                '<controller:\w+>/<id:\d+>' => '<controller>/save-as-new',

                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',

                '<action:(contact|captcha)>' => 'site/<action>'

            ],

        ],

        'urlManagerBackend' => [

            'class' => 'yii\web\UrlManager',

            'baseUrl' => '/yii2_perswitch/backend/web/index.php',

        ],

    ],

    'params' => $params,

];



Well, I wonder where to start …

  1. You posted 2 screenshots at the beginning. Is the following understanding of mine correct or not?

1-1) Image #1 shows what you get now. But it’s not what you want.

1-2) Image #2 shows what you want to get. But it’s not exactly what you want, because it has a wrong title.

  1. Do you already have a page that exactly satisfies your needs? I mean, #2 with a correct title.

  2. If you do, what is the route ( "module/controller/action") of that page?

I believe he is expecting the page to look like shot #1, but when he clicks on the link it display content shown shot #2. I think it is more related to the layout/view problem.

Image1 named attachement_0 shows up what I get in the moment, but this is a mixture of two tables. This is,what I don’t want

Image2 named attachement_1 sows up,what I want. But I get this content first after having reloaded page manually in Browser.

Following link will work,as it should work. But this link doesn’t work in anonymous function

Link:




Html::a(Yii::t('app', 'Dateianhang'), 'dateianhang', ['class' => 'btn btn-danger']);



Link in anonymous function,which won’t do the same as upper link. But it should do exactly the same- that’s my intention





    [

        'class' => 'yii\grid\ActionColumn',

        'template' => '{anlagen_index},

        'buttons' => [

            'anlagen_index' => function ($url) {

                $url ='dateianhang';

   			return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', $url, ['title' => 'Anlagen anzeigen']);

          

            },

        ],

    ],



So,my mayor problem is return value in anonymous function. This won’t load script propably!!

These are modul definitions in config:

Modul:




'dateianhang' => [

'class' => 'frontend\modules\dateianhang\Dateianhang'

],



[size="2"]urlManager(source):[/size]




 'bewerber' => 'bewerber/bewerber/index'



[size="2"]urlManager(final destination):[/size]

[size="2"]




'dateianhang' => 'dateianhang/dateianhang/index'



[/size]

No. It is working by chance.

It should be written like the following:




Html::a(Yii::t('app', 'Dateianhang'), ['dateianhang/dateianhang/index'], ['class' => 'btn btn-danger']);



In the same way, you should write like the following in your anonymous function:




    'buttons' => [

        'anlagen_index' => function ($url) {

            return Html::a('<span class="glyphicon glyphicon-paperclip"></span>',

                 ['dateianhang/dateianhang/index'],

                 ['title' => 'Anlagen anzeigen']);

        },

    ],



You are using a relative url in string format for the 2nd parameter of Html::a(), but you should use a route in array format for it.

Generally speaking, we should not use a url in string format to render a link, unless it point to an external url. All the internal links in the app should be constructed using routes.

This will not work! I already explained, that I am using prettyUrl=> true. Furthermore, I published all my Urlmanager rules. So, if I code as U suggested, following url will be created:




http://localhost/yii2_perswitch/frontend/web/index.php/bewerber/dateianhang/dateianhang/index



This is nonsense. The only url,which is valid will be like this:




http://localhost/yii2_perswitch/frontend/web/index.php/dateianhang



If I try like this, I also won’t get any valid url:




return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', [\Yii::$app->urlManager->baseUrl . '/index.php/dateianhang'], ['title' => 'Anlagen anzeigen']);



also this will fail:




 return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', [Url::toRoute('dateianhang/dateianhang/index')], ['title' => 'Anlagen anzeigen']);



I ergo can’t define url as an array!!

[b]

[/b]Please note, that script being loaded by link is in another folder, using another controller/model. The only common ground is application in frontend

For upper reasons, following code of umneeq (see above) already had to be discarded:





[

        'buttons' => [

            'anlagen_index' => function ($url, $id) {

                return Html::a('', ['/url/to/any/point'], ['title' => 'Anlagen anzeigen']);

            },

        ],

    ],



"if"? "will be"?

Why don’t you say “When I coded as U suggested, following url was created”?

Did you actually try my suggestion?

I do understand it.

Of course, not.

It’s for sure, of course.

It’s just because you don’t understand the difference between the string format and the array format for the 2nd parameter of Html::a().

I know. But it’s not a problem at all.

umneeq’s code is a bit misleading. It should have been like this:




    return Html::a('', ['route/to/anypoint'], ['title' => 'Anlagen anzeigen']);



Or, rather




    return Html::a('', ['module/controller/action'], ['title' => 'Anlagen anzeigen']);



Do you understand what "route" means and the way yii creates urls from a route?

Please read the following section of the guide.

Guide > Routing and URL Creation

http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html

I already know this guide.i’ve read it month ago defining url-rules in manager. Nevertheless, if I coded defining url as an array in 2nd parameter of a-Tag…




    [

        'class' => 'yii\grid\ActionColumn',

        'template' => '{anlagen_index} <br>{anlagen_create} ',

        'buttons' => [

            'anlagen_index' => function ($url) {

                return Html::a('<span class="glyphicon glyphicon-paperclip"></span>', ['dateianhang/dateianhang/index'], ['title' => 'Anlagen anzeigen']);

            },

        ],

    ],



I had gotten following error:




Not found(404)

http://localhost/yii2_perswitch/frontend/web/index.php/bewerber/dateianhang/dateianhang/index



[size=2]So, problem still is unsolved[/size]

Ah, OK. I was wrong.

Try this:




['/dateianhang/dateianhang/index']



I forgot to prefix ‘/’ for the route to indicate that it is an absolute one.

Without ‘/’ prefix, ‘dateianhang/dateianhang/index’ will be considered as a relative route from the current route.

You need to use an absolute route when you refer to a route in another module.

This will give me the same result compared with defining url as string

I will get content of table dateianhang, but header is of table bewerber. Look at attachement,please (view_link_0.jpg)

[size="2"]I have to reload script manually in Browser in order to get content and header propably(view link_1.jpg) [/size]

As it seems,link being defined in anonymous function won’t load whole script, just content of table dateianhang is shown up. Setting link outside of Gridview will load script as it should be…

This is [color="#212121"][font="inherit"][size="2"]unacceptable[/size][/font][/color][size="2"]. So, this problem is still unsolved…[/size]

quick question do you have pjax enabled by any chance in your gridview? I think its pjax that is messing up your layout.

Can you paste your view code here entire file.

Yes. I use pjax.




Pjax::begin();

Pjax::end();

?>



exactly that is where your problem is, try it without pjax first.

Great - Not Bingo

U are right. After having removed static class Pjax, script will be loaded propably. This will mean,that I will get traffic,which actually shold be avoided. I have to ask my chief for another design, if he wants to use AJAX,right?

Where does ur guess come from? Without U, I never got this depenencie(s). Thx a lot for this!!

I think you can suppress pjax for the specific link with ‘data-pjax’ attribute, while keeping pjax enabled for the whole page:




    'buttons' => [

        'anlagen_index' => function ($url) {

            return Html::a('<span class="glyphicon glyphicon-paperclip"></span>',

                 ['/dateianhang/dateianhang/index'],

                 ['title' => 'Anlagen anzeigen', 'data' => ['pjax' => '0']]);

        },

    ],



[edit]

See also API for yii\widgets\Pjax

http://www.yiiframework.com/doc-2.0/yii-widgets-pjax.html

I have been bitten by this like 100 times, I used to do ruby work at my previous company (yellowpages) we used pjax heavily. I figured it when i saw your namespaces and I noticed yii\widgets\Pjax I knew it had to be pjax.

I am glad you got it working. I think you can still get it working with pjax enabled you have to tune a bit as softark pointed out.