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

Enabled useAppViewPath require copy all view under @app/views #1816

Closed boehsermoe closed 6 years ago

boehsermoe commented 6 years ago

When I want override the detail view of the news module I just need to enabled useAppViewPath and create @app/views/news/detail.php. But also I need the index view @app/views/news/index.php because the module search only in @app/views/news. In this case it is no so big problem to copy index.php into app views. But when a module have much more views I get more difficulties.

Maybe the yii2\base\View::findViewFile should override. There could check $contect->useAppViewPath is enabled and if a file exists in the @app/views/news use it else use @vendor/luyadev/luya-module-news/frontend/views/default/index.php.

nadar commented 6 years ago

So basically you want to be able to partial override view files.

The solution you provide is "very magic" - i total understand your approach but its maybe hard find out in debugging situation. Maybe it could be done be configure the module like this:

public $useViewFiles = ['controller/action1' => '@app/views/action1.php', 'controller/foo-bar-action' => '@app/views/foobar-action-with-some-text.php'];

just an example. But in this case you can leave useAppViewPath = false and define the lookup path for all actions. What do you think?

boehsermoe commented 6 years ago

Ok, but maybe instead map the action to a view it should map a module view to app view. Like $controllerMap for controllers same thing for views.

public $viewMap = ['action/view' => '@app/views/module/action/view', 'foo-action/bar-view' => '@app/views/module/foo-action/bar-view'];

nadar commented 6 years ago

So viewMap could have wildcards assuming we have controller/action routes. I the example code below i assume we have a DefaultController, LoginController and both of them have two actions index and info.

$viewMap = ['*' => '@app/views/mymodule'], // maps all views of all controllers and actions to the given folder
$viewMap = ['default/*' => '...',] //  maps all views from the default controller to the given folder
$viewMap = ['default/index' => '...', 'login/info' => '...'] // maps only default/index and login/info to the given folder.

What do you think?

boehsermoe commented 6 years ago

Sorry for my late answer. Your solution look fine. I think the same could be good to override block views. Because I want to customize the template for a core block. So could we make two properties $actionViewMap and $blockViewMap?

One more thing: Is there any idea to extend/override a block? For example I want to add a config option in LinkButtonBlock and call the new option in my customized template.

nadar commented 6 years ago

We already introduce the option to override the block views and it was a horrible and very inconsistency solution. Because when ever we introduce new things, or rewritten methods in blocks, all the override views where broke. In the end we where scared from touching the blocks, which is definitely the most worst scenario for something which should evolve from time to time and get more features. So what we then started to recommend ourselves and other people. Just create your own block, in general it takes just a few minutes and you have full control now and in the future.

So therefore i don't think we will re-introduce an option again to override block views (also not for grid views, for the same reason).