Common practice to pass class names to views in Laravel - php

I have a controller method that passes model names and class names to a view. These classes are then instantiated in another controller method. In this case I'm using the Laravel Excel package.
public function index()
{
$exports = [
'Model name 1' => TestExport::class,
'Model name 2' => AnotherExport::class
];
return view('export', compact('exports'));
}
public function download(string $collection)
{
return Excel::download(new $collection(), 'Export.xlsx');
}
My view file then redirects to the download controller method with the specific class name.
#foreach($exports as $name => $collection)
Download
#endforeach
Since I'm learning design patterns, and noticed it would break the DRY rule, I didn't want another controller method or every different Excel file that I downloaded.
Is this good practice or can this be done better?

You can make $exports common to both method and not accept class name from the request.
const EXPORTS = [
'export_name_1' => TestExport::class,
'export_name_2' => AnotherExport::class,
];
public function index()
{
return view('export', compact(self::EXPORTS));
}
public function download(string $collection)
{
if (!isset(self::EXPORTS[$collection]) {
return 'error';
}
$className = self::EXPORTS[$collection];
return Excel::download(new $className(), 'Export.xlsx');
}
Never let request manipulation break your code. For the export_name, you can simply use integers or the array simple index.
View
#foreach($exports as $name => $collection)
Download
#endforeach

To me it feels like your solution is mixing view logic with controller logic.
A controller should handle an incoming request, fetch the right data and then form a response.
A view should handle any logic based on the given data in order to render the output page.
In your case the data you pass to the view is not retrieved, it's just a static list which could be kept in the view just as well. A better solution would be to either:
Have a list of routes (not a list of classes) in your blade template and iterate over that instead.
If the list of routes is not that long, hardcode the whole list instead of making it dynamic.
The main reason for doing so would be to prevent a lot of 'magic' code. For instance in your blade template you expect exports to be an array which contains a list of classes which HAVE to be controllers and which HAVE to have a download function otherwise the code breaks.
An example for solution 1 would look something like:
<?php
// You could consider moving $routes to the controller
$routes = [
action('ExportController#download', TestExport::class),
action('ExportController#download', AnotherExport::class),
];
#foreach($routes as $route)
Download
#endforeach

Related

How to make the controller data override view controller in Laravel?

To build a sidebar that has a lot of dynamic data on it I learned about View composers in Laravel. The problem was that View Composers trigger when the view loads, overriding any data from the controller for variables with the same name. According to the Laravel 5.4 documentation though, I achieve what I want with view creators :
View creators are very similar to view composers; however, they are
executed immediately after the view is instantiated instead of waiting
until the view is about to render. To register a view creator, use the
creator method:
From what I understand, this means if I load variables with the same name in the controller and the creator, controller should override it. However this isn't happening with my code. The view composer:
public function boot()
{
view()->creator('*', function ($view) {
$userCompanies = auth()->user()->company()->get();
$currentCompany = auth()->user()->getCurrentCompany();
$view->with(compact('userCompanies', 'currentCompany'));
});
}
And here is one of the controllers, for example:
public function name()
{
$currentCompany = (object) ['name' => 'creating', 'id' => '0', 'account_balance' => 'N/A'];
return view('companies.name', compact('currentCompany'));
}
the $currentCompany variable in question, for example, always retains the value from the creator rather than being overridden by the one from the controller. Any idea what is wrong here?
After some research, I realized my initial assumption that the data from the view creator can be overwritten by the data from the controller was wrong. The creator still loads after the controller somehow. But I found a simple solution.
For those who want to use View composers/creators for default data that can get overwritten, do an array merge between the controller and composer/creator data at the end of the boot() method of the composer/creator, like so:
$with = array_merge(compact('userCompanies', 'currentCompany', 'currentUser'), $view->getData());
$view->with($with);

Passing variable from backend to frontend laravel

I have 5 controllers and 5 models and they are all related to backend. I can easily output data in the backend but I need to that for the frontend as well. Not all of course but some of them.
For example I have controller called BooksController:
public function getBooks(Request $request)
{
$books = Books::all();
return view('backend.books.show', compact('images'));
}
So this will show it in backend without any problems but what I want is for example to loop through all the books and show their images in welcome.blade.php which doesn't have controller.
And also to pass other parameters to that same view from different controllers.
Is this is possible?
Thank you.
You are having an error because you did not declare the variable $image
public function getBooks(Request $request)
{
$books = Books::all();
$images = array_map(function($book) {
$book->image;
}, $books);
return view('backend.books.show', compact('images'));
}
It sounds like you are potentially caught up on some terminology. In this case, it sounds like backend is referring to your admin-facing interface, and frontend is referring to your user-facing interface.
You also seem to be locked on the idea of controllers. Unless the route is verrrrrry basic, create a controller for it.
Have a controller for your welcome view, for your admin view, basically (with some exceptions) a controller per resource or view is fine.
In this case, you would have one controller for your admin book view, and a seperate controller for your welcome view. Both of which would pull the books out of the db and render them in their own way

Yii. Render can work as renderPartial too?

In the past, with Yii 1, I use to load partial views with "renderPartial", if you did not use renderPartial, it loaded the "partial view" with layout. Now in Yii2, method renderPartial has been moved to the controller, BUT you also can do:
<?= $this->render('_form', [
'model' => $model,
]) ?>
Inside another view, and it works same way as renderPartial, what kind of sorcery is this? :D. I guess in some level Yii2 checks if it is being loaded inside another view and uses renderPartial but I have not been able to find it. Anyone has found this?
I came to this question, because I was reviewing Pjax, playing with it and I saw a couple examples where the controller used return $this->render but Pjax still worked, as far as I know Pjax stops working if <html> tag or error has been found on the response, am I right?
All the controller and view render() methods are essentially using the same method, part of the view model. It's a key concept in DRY (don't repeat yourself) The view model has this method;
public function render($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
return $this->renderFile($viewFile, $params, $context);
}
All it does is render the view file specified by $view.
Now in the controller, you have two methods; render() and renderPartial(). The difference is just that in one a layout is applied, and in the other it isn't. The code looks like this;
public function render($view, $params = [])
{
$content = $this->getView()->render($view, $params, $this);
return $this->renderContent($content);
}
The first line in the method does two things. Firstly it gets the view object, then, by chaining, it uses the render() method of that view object to generate the html from that view.
The second line then passes that content on to the renderContent() method of the controller, which applies any layout to the content.
The code for renderPartial() is this;
public function renderPartial($view, $params = [])
{
return $this->getView()->render($view, $params, $this);
}
As you can see, it's exactly the same as the render() method of the controller, except that it doesn't pass anything to the renderContent() method of the controller, it just outputs it.
As for the pjax part of your question, I'm guessing that as long as your pjax doesn't generate any substantial errors, it will work whether you are using $controller->render(), $controller->renderPartial() or $view->render().

Laravel Routing to same view different function

I am trying to return values from a MySQL database into a datatable using Chumper. I am trying to obtain values from 2 tables; but I am having what I believe to be a routing issue as I do not know how to include 2 controller functions to the same view ie in my search view I want to render the result of 2 functions like : for a deeper understanding of what I am trying to accomplish see question : Laravel Chumper Datatable getting data into one datatable from multiple MySQL tables
Route::get('search', array('as' => 'instance.search', 'uses' => 'CRUDController#instances'));
Route::get('search', array('as' => 'accid.search', 'uses' => 'CRUDController#account_ids'));
any ideas ?
Only a single route will ever match any given URL requested of the system. I believe what Laravel will do is choose the second one (as in the second definition will overwrite the first).
You have some options here. All I can really tell from your question is that you want two methods to be executed when that route gets hit. This is indirectly possible, consider:
Route::get('search', 'MyController#instances');
class MyController extends Controller
{
public function instances()
{
$mydata = $this->account_ids();
$myotherdata = $this->getOtherData();
return View::make('myview')
->with('mydata', $mydata)
->with('myotherdata', $myotherdata);
}
private function getOtherData() { /* ... */ }
}
This isn't really clean, though, and eventually will lead to convoluted controller logic which is an anti-pattern in MVC. Fortunately, Laravel let's you use View Composers, which can greatly clean up your controller logic:
public function instances()
{
return View::make('myview');
}
Wow. Nice and simple. Now the view composer part:
// Inside of a service provider...
View::composer('search', 'App\Http\ViewComposers\AViewComposer');
use View;
class AViewComposer
{
public function compose(View $view)
{
$view->with('instances', $this->instances());
$view->with('accountIds', $this->accountIds());
}
public function instances()
{
// generate your instance data here and return it...
return $instances;
}
public function accountIds()
{
// generate your account id data here and return it...
return $accountIds;
}
}
You could take this a step further and inject another class into the constructor of this view composer to completely off-load the responsibility of determining what 'instances' and 'account ids' actually mean, should you need that same functionality elsewhere. This will help you keep your code extremely DRY.
You are having those 2 routes but with same domain so the second one is running over the second one.
Or you change the URL of on of them or you change the method for post instead of get

Laravel: Include view with controller logic

I come from the symfony (v1) world, where we had two types of partial rendering: partials, which loaded the same as:
#include('some.view')
and controllers, which would act the exact same way but would be ran with some controller logic behind hit. So, calling the same as above would first go to the matching some.view controller, and operate with the logic it had.
I'm trying to do the same with Laravel. Basically, I have this:
#foreach($array as $thing)
#include('controller.like.view', array('thing' => $thing))
#endforeach
... and I'd like my included view to run something like this (this is just an example, the actual code is a lot more complicated, otherwise I'd just write it with an if clause in Blade):
...
if ($thing%2) {
return 'a';
}
return 'b';
... so that only 'a' or 'b' would be printed in my loop. What's the best way to achieve this without having a bunch of PHP code in a Blade template?
Why not just like that?
#foreach($array as $thing)
#if($thing%2)
a
#else
b
#endif
#endforeach
In general though, it's mostly a good way to prepare the data in your controller before passing it to the view. This way the view is just for presenting the data.
You could also write a little helper function or even a full class (with optional Facade for easy access) But it really depends on your needs
Update
I'm not sure this is the best solution for you but it's the only one I can think of.
Put as much of the logic as you can in your "thing" class and then use that in the included view. Here's an example:
class Thing {
public function isA(){
// do the magic
return true;
}
}
View
#if($this->isA())
a
#else
b
#endif
Update 2
Or to make a bit more like the controller from symfony you described:
class Thing {
public function getVars(){
// do stuff
return array(
'all' => 'the',
'vars' => 'you',
'need' => 'in',
'the' => 'view'
);
}
}
And then when you include the view
#include('item', $thing->getVars())

Categories