Laravel 5 How to follow DRY in my simple controller logic? - laravel-5

I have some simple logic in my Controller. But I often use it in other method. Here is an example.
In my controller.
public function method1()
{
if(isset(Auth::user()->showroom->name)){
$showroomName = Auth::user()->showroom->name;
}else{
$showroomName = "Belum Ada Izin";
}
return view('method1view', compact('showroomName'));
}
public function method2()
{
if(isset(Auth::user()->showroom->name)){
$showroomName = Auth::user()->showroom->name;
}else{
$showroomName = "Belum Ada Izin";
}
return view('method2view', compact('showroomName'));
}
... so on
How I can follow DRY principle in my case?
Any help will be appreciated.
Thanks in advance.

For cleaner and shorter syntax, use data_get helper:
data_get(Auth::user(), 'showroom.name', 'your default value');
This in most cases comes handy, but not always is the best way.
However, in your example it seems, that maybe in fact you'd like to simply share this variable across your views - use share or a View Composer described in the docs.

In your Showroom model you could include the following function:
public function getNameAttribute($value)
{
return isset($value) ? $value : 'Belum Ada Izin';
}
That's assuming you want that behavior everywhere showroom->name is used. If not, make the function getNameWithDefaultAttribute, check $this->name in the body, and reference it in the controller like Auth::user()->showroom->name_with_default

Related

Automatically Load/Show Laravel View File

I am thinking of any techniques of autoloading the view files according to url.
For example:
public function addProducts()
{
return view('admin.addProducts');
}
public function editProducts()
{
return view('admin.editProducts');
}
public function allProducts()
{
return view('admin.allProducts');
}
Here, the Controller's method name is identical to view file name. So, I am thinking, if it is possible to load the view files without writing same kind of method again and again.
Enlighten me.
If your route only needs to return a view, you may use the Route::view method.
For example:
Route::view('/welcome', 'welcome');
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
read more here
It's the call PHP magic, man. https://repl.it/#Piterden/PHP-call-magic?language=php
public function __call($method, $parameters)
{
if (str_contains($method, 'Product')) {
return view("admin.{$method}");
}
}
btw, it's not a good practice for controller.

Returning same variable to every controller in laravel

I need to send the same result to almost every view page, so I need to bind the variables and return with every controller.
My sample code
public function index()
{
$drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
$locations = Location::get();
return view('visitor.index', compact('drcategory','locations'));
}
public function contact()
{
$drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
$locations = Location::get();
return view('visitor.contact', compact('drcategory','locations'));
}
But as you see, I need to write same code over and over again. How can I write it once and include it any function whenever I need?
I thought about using a constructor, but I cannot figure out how I can implement this.
You are able to achieve this by using the View::share() function within the AppServicerProvider:
App\Providers\AppServiceProvider.php:
public function __construct()
{
use View::Share('variableName', $variableValue );
}
Then, within your controller, you call your view as normal:
public function myTestAction()
{
return view('view.name.here');
}
Now you can call your variable within the view:
<p>{{ variableName }}</p>
You can read more in the docs.
There are a few ways to implement this.
You can go with a service, a provider or, like you said, within the constructor.
I am guessing you will share this between more parts of your code, not just this controller and for such, I would do a service with static calls if the code is that short and focused.
If you are absolutely sure it is only a special case for this controller then you can do:
class YourController
{
protected $drcategory;
public function __construct()
{
$this->drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
}
// Your other functions here
}
In the end, I would still put your query under a Service or Provider and pass that to the controller instead of having it directly there. Maybe something extra to explore? :)
For this, you can use View Composer Binding feature of laravel
add this is in boot function of AppServiceProvider
View::composer('*', function ($view) {
$view->with('drcategory', DoctorCategory::orderBy('speciality', 'asc')->get());
$view->with('locations', Location::get());
}); //please import class...
when you visit on every page you can access drcategory and location object every time
and no need to send drcategory and location form every controller to view.
Edit your controller method
public function index()
{
return view('visitor.index');
}
#Sunil mentioned way View Composer Binding is the best way to achieve this.

Best practices to define response in action controller Laravel

I have a method inside PostController
class PostController extends Controller {
public function index() {
$posts = Post::all();
return response($posts);
}
}
Two way:
class PostController extends Controller {
public function index() {
$posts = Post::all();
return $posts;
}
}
Both work fine but which way is better and more correctly?
I personnaly prefer this version:
return \Response::json($data);
Because it makes clear that the response is actual json data.
Just make sure your code is understandable by someone new on your project.
If you are writing an API project, where everything is always returned in json, simply return the model because you don't have to make the reader learn that it's JSON because everything is in JSON everywhere.
On the other hand, if it's some sort of mixed project (some routes return view, JSON, XML, whatever), try to make is as obvious as you can that this specific route return JSON data and nothing else.
Also as stated in comments, stay consistent. The shorter isn't the better. The better is the one that is simple to read and give enough info about what's going on.
It doesn't matter actually.. its depends on your desire and consistency..
return Response::json($model);
//or
return response()->json($model);
//or
return $model;
it give you same output..but if you using response, you can set the status code. most of the time, this reponse thing is used in API project.

How to get a value in controller from get method and return it back to another view?

The problem looks basic but it is really painful!
I'm using get method and getting value in controller and I want the same value to return in another view.
How can I do that???
Please help!!!
This is my function from controller:
public function guest(){
if (Input::get('Cash On Delivery')){
$get = Input::get('Cash On Delivery');
return Redirect::to('guest/guestview/'.$get);
}
Well, with regards to your answer, using $_REQUEST directly is not Laravel's way of doing things :(
I believe this is better
public function guest(Request $request)
{
if ($request->payment_method == ('Cash On Delivery'))
{
return view('guest/guestview', ['guest'=>$request->payment_method]);
}
}
Ok Guys, I figured it out,
Just do this below.
public function guest(Request $request){
if ($request->payment_method == ('Cash On Delivery')){
$get = $_REQUEST['payment_method'];
return view('guest/guestview', compact('get'));
}

Is there any decent way to Decorate models returned from a Magento `[model]_load_after`event?

I'm trying to overwrite some methods in models, and I'm on a mission to avoid overwrites and rewrites of models for maximum compatibility with other modules.
I figured the best way would be to simply decorate models after they are loaded from Magento, however as far as I can tell because of the way the observer pattern in Magento is written it's impossible to accomplish this. ( As Magento always returns the reference to $this ), and the lack of interfaces might also cause trouble later down the road? See this partial of Mage/Core/Model/Abstract.php
/**
* Processing object after load data
*
* #return Mage_Core_Model_Abstract
*/
protected function _afterLoad()
{
Mage::dispatchEvent('model_load_after', array('object'=>$this));
Mage::dispatchEvent($this->_eventPrefix.'_load_after', $this->_getEventData());
return $this;
}
My question boils down to the title, is there a decent way of accomplishing this?, or am I simply stuck with rewrites :(?
The path I would like to take is;
On event [model]_load_after
return new Decorator($event->getObject())
Where the decorator class in my case would be something like;
public function __construct(Mage_Sales_Model_Order_Invoice $model)
{
parent::__construct($model); // sets $this->model on parent class, see below
}
// overwrite the getIncrementId method
public function getIncrementId()
{
return '12345';
}
// partial of parent class
public function __call($method, array $args)
{
return call_user_func_array(array($this->model, $method), $args);
}
And just some pseudo-code for extra clarification;
$model = Mage::getModel('sales/order_invoice')->load(1);
echo get_class($model);
Namespace_Decorator **INSTEAD OF** Mage_Sales_Model_...
echo $model->getIncrementId();
'12345' **INSTEAD OF** '1000001' ( or whatever the format might be )
Thanks for your time reading / commenting, I really hope there actually is a way to accomplish this in a clean fashion without making use of code overrides or rewrites of models.
Edit: extra clarification
Basically what I would like is to return an instance of the Decorator in a few cases, the sales_invoice being one of them and customer the other. So when any load() call is made on these models, it will always return the instance of the Decorator instead of the Model. Only method calls that the decorator overrides would be returned, and any other method calls would "proxied" through __call to the decorated object.
I'm not sure if I got your question right but here goes.
I think you can use the event [model]_load_after and simply do this:
$object = $event->getObject();
$object->setIncrementId('12345');
Or if you want to use a decorator class make it look like this:
public function __construct(Mage_Sales_Model_Order_Invoice $model)
{
parent::__construct($model);
$model->setIncrementId($this->getIncrementId());
}
public function getIncrementId()
{
return '12345';
}
I know that this is not exactly a decorator pattern but it should work.
I know that when adding a new method to the 'decorator' class you need to add it to attach data to the main model.
This is just my idea. I haven't got an other.
[EDIT]
You can try to rewrite the load method on the object to make it return what you need. But I wouldn't go that way. You can end up screwing a lot of other things.
I don't think there is an other way to do it because load always returns the current object no mater what you do in the events dispatched in the method. see Mage_Core_Model_Abstract::load()
public function load($id, $field=null)
{
$this->_beforeLoad($id, $field);
$this->_getResource()->load($this, $id, $field);
$this->_afterLoad();
$this->setOrigData();
$this->_hasDataChanges = false;
return $this;
}
By making it return new Decorator($this), you might achieve what you need, but just make sure that when calling $model->doSomething() and doSomething() is not a method in your decorator you still end up calling the original method on the model.

Resources