Asset for multiple views in Yii2 - php

So i am into the challenge of in passing the asset for a number of different views.
As far as I am concerned, there are some ways, apart from adding
use app\assets\SomeAsset;
SomeAsset::register($this);
to the layout page or calling out this method.
Although, I am interested, are there any more flexible ways of passing an asset or a group of assets to the whole crud model at once, for example.

Wiggling around me and my colleague found out that this way works as I wanted to:
use app\assets\SomeAsset;
//...Some other dependencies
class DefaultController
{
// add this at the beginning of class in the controller
public function beforeAction($action)
{
SomeAsset::register($this->view);
return parent::beforeAction($action);
}
}
So this might help if you want to pass asset to all files that are under DefaultController control.
Hope this will be useful to somebody.

Related

How to access data some specific Controllers in Laravel?

For sharing data with all views I read in documentation we can create
View::share('key', 'value');
I found that also we can make View Composer to share data with only some specific views.
But how to actually share data with only some specific controllers?
Few controllers we list to have injected some variables, objects arrays etc ready to use.
One Idea that comes to my mind is to create for them middleware... But I don't think it should be done something this way
Looking for this in documentation and web but cannot found, so how actually share data only with some specific controllers that we want to?
Maybe this will help for you:
Some snippets from conetix.com.au/blog/simple-guide-using-traits-laravel-5
You can use use ExampleCode; in the controller you wish
<?php
namespace App\Traits;
trait ExampleCode
{
public function asd()
{
return [1,2,3];
}
}
namespace App\Something;
use App\Traits\ExampleCode;
class Someclass
{
use ExampleCode;
public $yourarray;
public function __construct()
{
$this->yourarray = $this->asd();
}
public function hi(){
dd($this->yourarray);
}
}
If multiple controllers need to have access to the same data, then you may want to consider creating a base controller that they inherit from and setting that data in the constructor of the controller.
Alternatively you can store it in the session and retrieve it from there in the controllers that need it.
Maybe it's better to use decomposition for this purpose. Write some service class for your data, bind it to your service container and then inject it into controllers constructor or action methods.

codeigniter: one controller several actions

I'm using codeigniter. I'm currently working on my routes and controller.
Last week, I explored symfony2 and I liked something:
class DefaultController extends Controller
{
public function indexAction()
{
return $this->render('LVIndexBundle:Default:index.html.twig');
}
public function servicesAction()
{
return $this->render('LVIndexBundle:Default:services.html.twig');
}
public function shoppingAction()
{
return $this->render('LVIndexBundle:Default:services.html.twig');
}
In the controller, each action renders a view.
I would like to do the same in codeigniter -> get several functions / actions leading to distinct views.
I'm new to codeigniter. So far, I understood that 1 controller = 1 view.
I'd like to get 1 controller = several functions for several pages. Otherwise, that would be a lot of pages.
Thanks very much for your help!
Based on my experience(s):
In CI, a controller can has more than 1 view php file
ex (function indexAction as a controller function):
public function indexAction()
{
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
}
In Symfony and Codeigniter the controllers are just classes that can hold multiple methods. The methods that are called (called Actions in Symfony) are the place that decides which view(s) must be rendered. One of the main differences between Symfony and CI controllers are the routings that Symfony uses. The routes makes Symfony more flexible then CI.
take a look for an explanation of CI controllers
p.s. Symfony is much more advanced then CI. I like to advise you Symfony :-)
Its absolutely not true that there should be one view per controller. even something as simple as validating a form - you will call different views depending on if the validation passes or not.
Strongly suggest you step through the Tutorial in the codeigniter manual. this is going to answer a lot of questions and its very practical.

How to pass data to layout in Laravel regardless of controller?

Let's say I got ControllerA and ControllerB which both implement the same layout. Now I want to pass data to layout, for example, a message that should appear in layout no matter which controller implements it. If I had only 1 controller, I would do something like:
class Controller extends \BaseController {
public function setupLayout() {
View::share('message', 'Hello world!');
}
// further methods
}
However, when I want multiple controllers to implement a layout, I have to do this in every controller, which doesn't sound reasonable. So I wanted to ask, is there any native way in Laravel to pass data to layout and not to copy code in every controller.
Thanks in advance!
For those cases I would recommend a view composer, where you can set them for more than one view (or layout) or just all of them:
View::composer(['store.index', 'products.*'], function($view)
{
$view->with('model', 'one');
$view->with('colour', 'black');
});
You can put that in your routes file, filters file or, like, me, create a app/composers.php and load by adding
require app_path().'/composers.php';
To your app/start/global.php.

Creating a re-usable controller component in Lithium

I am currently developing a Lithium application and have come across a function that I have written that I would like to use across multiple Controllers.
I obviously don't want to have the function in each controller. What is the standard way of creating a re-usable component in Lithium?
The lack of search facility on their documentation is making it difficult to find any specifics.
You could try extending controller. Extending controllers is not that bad according to core devs. If that is not and option you could extract your code into a plugin, still some code in controller though.
All you have to do is create a extensions/action/Controller.php and have your controllers extend that.
In your extensions/action/Controller.php
<?php
namespace app\extensions\action;
class Controller extends \lithium\action\Controller {
protected function _init() {
parent::_init();
//add your functionality here
}
}
?>
And then, your controller has to extend the above mentioned base controller: class MyController extends \app\extensions\action\Controller {
I think this is not a Lithium-specific thing. You could either inherit from the Controller and make your own base controller, but you can also create arbitrary classes that hold your functionality. Don't let a framework inhibit you =)
Regarding the documentation: I usually google in the sense of "<keywords> site:lithify.me"

How to avoid large actions classes with Symfony 1.4

working on a project at the minute and don't feel the code is as well structured/decoupled/maintainable as it should be.
I think the main problem is using one actions class to do the brunt of the work of my system. But what are the alternatives? My other modules are pretty basic and separate.
Should the model contain more business logic that I have in my controller (at the minute the model file for the actions class in question is more or less empty, the Table model file doesn't have much either).
I was reading http://www.slideshare.net/nperriault/30-symfony-best-practices which has some good points, one of them being avoiding having large actions classes but doesn't really tell you how it should be done.
Should I break up my actions class and have different files in /apps/frontend/module/myModule/lib/. ?
The problem is I have another actions class that has to reuse some functionality from the class mentioned above... I really don't want to have duplicate code an am getting into a bit of a mess.
Some general pointers would be great, thanks
You can either keep all actions in one file, or put each action in its own file.
So, either
class mymoduleActions extends sfActions
{
public function executeIndex($request)
{
// ...
}
public function executeList($request)
{
// ...
}
}
or
class indexAction extends sfAction
{
public function execute($request)
{
// ...
}
}
plus
class listAction extends sfAction
{
public function execute($request)
{
// ...
}
}
It's all in the documentation. You should then put reusable code in a parent XYZAction class that both action classes can extend.
You can always put code in apps/frontend/lib. Create classes that have the code that may be re-used and you can just call the methods from within your actions to avoid duplication

Categories