What is controller_group for in view call? - voltrb

I just wanted to understand better the view call in main.hmtl: what is the third parameter {controller_group: 'main'} for in:
{{ view main_path, "title", {controller_group: 'main'} }}
Are there other options or parameters that can be passed to view? Sorry if I've missed something in the docs.

controller_group makes it so multiple view bindings can share the same controller instance. You give it a unique string name between each view binding and it will only load up one. Otherwise each view (title and body for example) would get its own controller.

Related

pass parameters to current controller from _layout.cshtml

I use a _layout.cshtml file that have a language selection, when the user change language I need to only pass lang=gb. Since this layout is applied to several views/controllers I need to pass the lang parameter to the view/controller that is currently rendered. What helper method do I use then?
This post helped me out Getting controller name from razor
There I could get the current controller and pass the lang parameter like this..
href="~/#controllerName?lang=gb"

What's the alternative to Symfony render(controller) in Laravel

In Symfony I am using widget-like behaviour by calling controller method from twig
{{ render(controller(
'App\\Controller\\ArticleController::recentArticles',
{ 'max': 3 }
)) }}
It does the logic and returns another twig template, which is embeded here.
How in the Laravel do people solve this? I need this for displaying menu, responsive menu, product lists, breadcrumbs etc.
I have read about View composers and studied the documentation - but there's mentioned only how you can inject some variables into the view.
The same with using #inject() in blade.
But I want standalone widget (with own logic, data fetching...) with custom blade template embeded/inserted in any place I call them from.
Thanks for an advice.
Btw. It doesn't need to be Controller that I call, it could be a Service object. But the point is the same. I was personally calling these objects like _WidgetController (beginning with underscore - to tell me, they are not fully qualified views, but components/widgets). But I placed them into my Controllers folder.

How can I use 2 models in a single view?

I'm currently working on a homepage where I am showing the 'latest hauls' and 'latest finds' they are both separate models ofcourse.
Currently I am only showing the 5 latest hauls since I use the HaulController#getWelcome Controller so I can access $haul->title etc.
How would I be able to also access $finds->title?
Thanks for the help!
You can share a model to a view by using it inside controller function -
View::share('subadmin', App\Models\SubAdmin::class);
You can call the model in your view like
{{ \App\Model::function() }}
So you can have {{ App\Model::orderBy('created_at', 'desc')->limit(5)->get() }}
You have a couple of options.
Option 1: A combined model and one view.
To do this create a new ViewModel and have LatestHauls and LatestFinds as properties on this model. You could set these properties in the controller.
On your view simply navigate down the viewmodel to the appropriate properties.
Option 2: Extend the above solution to use partial views. Create a partial view for each model. On the parent view call each partial view. This might be considered a better solution but option 1 will get you started.

Laravel include view

I want to include a view like so: #include(user.myview), but within this view I need UserController logic. So I thought about calling a route: #include( route('user.route') ) which calls a Controllerfunction and returns the view but that isn't working. Any Ideas how to deal with this problem?
You need to create view composer and use it to get the data.
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location.
Simply add a link in you view and include it in your desired location.
Link will have a route.
On clicking the link, controller method can be called. e.g. show_link.blade.php
In your show_link.blade.php view:
<a href= {{route('route-name')}} > Click here</a>.
This route will call a method via .
Route::get('/call/method', 'controller#your_method_name')->name('route-name');
In controller, method your_method_name that will look like this:
public function your_method_name()
{
return "show what you want to";
}

MVC3 RenderPartial and the ViewBag

I'll try to keep this brief:
In the view
#Html.RenderPartial("myview", Model.SubModel,
new ViewDataDictionary()
{
{ "thing", Model.Thing }
})
In the myview we see that "thing" is avaiable, i.e. this produces the value of Model.Thing in the myview view:
#ViewBag.thing
Great! Simple
But I need to do something with thing (and thing can't be part of SubModel by the way), i.e. access this in my view engine or ideally in the controller, e.g.:
public ActionResult myview(SubModelType vm)
{
var thing = ViewBag.thing; // oh dear this doesnt exist.. but is there when the view is rendered
So my question is, if the ViewBag is available in the resulting myview, it must be being passed around somehow in the httpcontext, or in the controllercontext somewhere right? Does anyone know why it's not available in the controller but is in the view and how I might be able to access this?
Edit
Sorry I missed a vital point here! It's when "myview" is posted back to the controller, invoking an action called myview where I would expect the ViewBag rendered to myview to be available. But of course it's not, it's been used in "myview" and that's it. So if I want to use it in the myview action I'm going to need to store in the ViewBag in that view OR set some value of the viewmodel so that it can be posed back to the action.
Does that make sense?
Sorry I missed a vital point here! It's when "myview" is posted back
to the controller, invoking an action called myview where I would
expect the ViewBag rendered to myview to be available.
Oh no, you cannot be possibly expecting anything like that. That's not how ASP.NET MVC works. When you send an HTTP request to a controller action the default model binder will intercept this request and look in the posted values from the request and bind them to your action argument:
[HttpPost]
public ActionResult myview(SubModelType vm, ThingViewModel thing)
{
...
}
This obviously assumes that those thing values were part of the original request. So for example if you submit a form you should include corresponding fields inside this form that the model binder could use.
Think of it that way: an ASP.NET MVC controller action could be invoked from any client. For example from an iPhone application. And as you know there's no such notion as ViewBag in iOS. All that will happen is that the default model binder will look at the POSTed values and attempt to hydrate the view models that your action is taking as arguments.
If on the other hand you cannot make those thing values as part of the request (by including corresponding input fields in the form) you could only send the ID of this thin from a hidden field and then inside your controller action use this ID to query your underlying data-store to retrieve the thing from the same place you retrieved it initially when you first rendered this form.
Some people might also suggest you storing the Thing inside the Session and then reading the value back from the Session in your POST action. I am not from those people. It's an alternative approach though.

Resources