Using traits is one of the best practices for OOP (Object Oriented Programming) and [SOLID Principles] in PHP

What are traits

Traits are a mechanism for reusing code in single inheritance languages ​​like PHP.

Traits are designed to reduce some of the limitations of single inheritance by enabling developers to freely reuse method sets across several independent classes living in different class hierarchies.

In simple terms, Traits are a set of methods to be included in another class. You can easily reuse this method for another class. Trait is writing the same code over and over again

1. Create Traits

app creates a folder in a directory called Traits and creates an ImageTrait in it

verifyAndUpload() lets us create a new trait with a function

<?php

namespace App\Traits;

use Illuminate\Http\Request;

trait ImageTrait {

    /**
     * @param Request $request
     * @return $this|false|string
     */
    public function verifyAndUpload(Request $request, $fieldname = 'image', $directory = 'images' ) {

        if( $request->hasFile( $fieldname ) ) {

            if (!$request->file($fieldname)->isValid()) {

                flash('Invalid Image!')->error()->important();

                return redirect()->back()->withInput();

            }

            return $request->file($fieldname)->store($directory, 'public');

        }

        return null;

    }

}

2. Create the controller that will use the ImageTrait

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Item;

class ItemController extends Controller
{
     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('imageUpload');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->all();

        $input['image'] = '';

        Item::create($input);

        return back()
            ->with('success','record created successfully.');

    }
}

3. Insert and use ImageTrait on the controller

use App\Traits\ImageTrait;
use ImageTrait;
$input['image'] = $this->verifyAndUpload($request, 'image', 'images');

full code


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Item;
use App\Traits\ImageTrait;

class ItemController extends Controller
{
    use ImageTrait;
     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('imageUpload');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->all();

        $input['image'] = $this->verifyAndUpload($request, 'image', 'images');

        Item::create($input);

        return back()
            ->with('success','record created successfully.');

    }
}

 

We have successfully created and applied Traits! You can now use it in your projects and improve the readability and stability of your code!
点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部