luyadev / luya

LUYA is a scalable web framework and content management system with the goal to please developers, clients and users alike.
https://luya.io
MIT License
812 stars 207 forks source link

Using multiple modules in project #1926

Closed eothein closed 5 years ago

eothein commented 5 years ago

What steps will reproduce the problem?

Generate links in the views/layouts/main.php file as follows for my own module, which I use in the navbar

$url = Url::toRoute(['/inschrijvingfrontend/toernooi/index',"toernooi" => $toernooi->id]);

Generate other menu-items in the navbar as follows:

<?php foreach (Yii::$app->menu->find()->container('default')->root()->all() as $item): ?>
 <li>
 <a href="<?= $item->link; ?>"><?= $item->title; ?></a>
 </li>
<?php endforeach; ?>

What is the expected result?

Correct url form's for the custom module and the contact module, e.g.

And this from any page.

What do you get instead? (A Screenshot can help us a lot!)

So it tries to contact the controller from the contact module, instead of the controller of the inschrijvingsmodule and hence the url is malformed.

LUYA Check ouput (run this script and post the result: [luyacheck.php]1: [in_array('mod_rewrite', apache_get_modules())] true

2: [ini_get('short_open_tag')] '' 3: [ini_get('error_reporting')] '22527' 4: [phpversion()] '7.1.23' 5: [php_ini_loaded_file()] '/etc/php.ini' 6: [php_sapi_name()] 'apache2handler' 7: [isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : unknown] 'Apache/2.4.34 (Unix) PHP/7.1.23'

Additional infos

Q A
LUYA Version Yii 2.0.017, "luyadev/luya-core" : "^1.0",
PHP Version PHP 7.1.23 (cli) (built: Feb 22 2019 22:19:32) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans
Platform XAMPP
Operating system OSX
nadar commented 5 years ago

Thanks for the detailed report, now i can see what you are doing.

  1. What is the reason for this?

$url = Url::toRoute(['/inschrijvingfrontend/toernooi/index',"toernooi" => $toernooi->id]);

I assume its a foreach? So your main intention is to have pages generated from the module?

  1. Have you added url rules for the inschrijvingfrontend module?

=> https://luya.io/guide/app-module-urlrules

  1. How have you integrated the contact form - with a block or as module page?
eothein commented 5 years ago

Dear @nadar

The reason for is that I want to generate a link to a page in the module (i.e. a page representing a tournament). I have a controller in the front-end which takes the $toernooi as id and then generates the page for that tournament.

I have not added url rules for the inschrijvingsfrontend module. Is this necessary when generating a module?

eothein commented 5 years ago

Ok so I added the following rule:

    public $urlRules = [
        ['pattern' => 'bp-detail', 'route' => 'inschrijvingfrontend/toernooi/index'], 

    ];

And this does the trick.

Although it is not clear to me why this must be done to get the modules working. I checked the Yii2 documentation for modules which states:

Like accessing controllers in an application, routes are used to address controllers in a module. A route for a controller within a module must begin with the module ID followed by the controller ID and action ID. For example, if an application uses a module named forum, then the route forum/post/index would represent the index action of the post controller in the module.

So I thought that the following code would render the correct link (which it did without the addition of the contact_form module).

 $url = Url::toRoute(['/inschrijvingfrontend/toernooi/index',"toernooi" => $toernooi->id]);
nadar commented 5 years ago

The problem is that you are using the LUYA CMS Module and the cms needs to catch ALL PATHS in order to work, for example we need to catch /xyz/y/z and pass it to the cms in order to display the correct page. So we have to differ between cms and module routes, therefore in this case an url rules is needed. So i can close the issue?

eothein commented 5 years ago

Hello @nadar , thanks for the explanation. I will indeed close the issue.

Maybe a change in the documentation/guide would be handy: i.e. when using own modules, adding an URL rule is necessary?

nadar commented 5 years ago

The url rule is only required if you DONT add the module as module-page or with module-block inside a page.

nadar commented 5 years ago

Because then you could use: https://github.com/luyadev/luya-module-cms/blob/master/src/helpers/Url.php#L30 or https://github.com/luyadev/luya-module-cms/blob/master/src/helpers/Url.php#L70

eothein commented 5 years ago

This is unclear to me.

I added both the contactModule and the inschrijvingsModule as a page.

When I delete the urlRoute e.g.

public $urlRules = [
        //['pattern' => 'bp-detail', 'route' => 'inschrijvingfrontend/toernooi/index'],  
    ];

then

$url = Url::toRoute(['/inschrijvingfrontend/toernooi/index',
                                        "toernooi" => $toernooi->id]);

rendered in the main.php file, when accessing http://beachpolo.test/contact renders the url http://beachpolo.test/contact/toernooi/index?toernooi=3

Which is not correct. Should be http://beachpolo.test/inschrijvingfrontend/toernooi/index?toernooi=1

What am I not getting?

nadar commented 5 years ago

try: toModuleRoute from cms url helper

eothein commented 5 years ago

Ok, so this does the trick. Well let me correct my tip for the documentation. Using the correct Url class, i.e. use luya\cms\helpers\Url; ;)