Iqbal
Iqbal

Reputation: 15

how to customize controller generator file in laravel 5.5

<?php

namespace DummyNamespace;

use Illuminate\Http\Request;
use DummyRootNamespaceHttp\Controllers\Controller;

class %%ModelName%% extends Controller
{
   /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function admin_index()
    {
        $data=$this->%%ModelName%%->find('all');
            $this->set('%%ModelName%%s',$data);
    }


    public function admin_add()
    {
        if($this->request->is('%%ModelName%%'))
        {
            $this->%%ModelName%%->create();
            if($this->%%ModelName%%->save($this->request->data))
            {
                $this->Session->setFlash('The %%ModelName%% has been created.');
                return $this->redirect(array('action'=>'index'));
            }
            else
            {
                $this->Session->setFlash("The %%ModelName%% haven't been created!");
            }
        }   
        $this->set('topics',$this->%%ModelName%%->Topic->find('list'));
    }


    public function admin_view($id)
    {
        $data=$this->%%ModelName%%->findById($id);
        $this->set('%%ModelName%%',$data);
    }


    public function admin_edit($id)
    {
        $data=$this->%%ModelName%%->findById($id);
        if($this->request->is(array('%%ModelName%%','put')))
        {
            $this->%%ModelName%%->id=$id;
            if($this->%%ModelName%%->save($this->request->data))
            {
                $this->Session->setFlash('The %%ModelName%% has been edited.');
                return $this->redirect(array('action'=>'index'));
            }
            else
            {
                $this->Session->setFlash("The %%ModelName%% haven't been edited!");
            }
        }
        $this->request->data=$data;
        $this->set('topics',$this->%%ModelName%%->Topic->find('list'));
    }


    public function admin_delete($id)
    {
        $data=$this->%%ModelName%%->findById($id);
        if($this->request->is(array('%%ModelName%%','put')))
        {
            $this->%%ModelName%%->id=$id;
            if($this->%%ModelName%%->delete())
            {
                $this->Session->setFlash('The %%ModelName%% has been deleted.');
                return $this->redirect(array('action'=>'index'));
            }
            else
            {
                $this->Session->setFlash("The %%ModelName%% haven't been deleted!");
            }
        }

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Upvotes: 1

Views: 59

Answers (1)

T&#252;rkalp
T&#252;rkalp

Reputation: 188

You can do it like following,

<?php

namespace App\Console\Commands;

use Illuminate\Routing\Console\ControllerMakeCommand;

class CustomMakeController extends ControllerMakeCommand
{
    // YOUR CUSTOM NAME
    protected $name = 'make:custom-controller';

    // YOUR CUSTOM STUB FILE
    /**
     * @return string
     */
    protected function getStub()
    {
        return STUB_URL;
    }

}

Also, you should register it to App\Console\Kernel.php like following,

protected $commands = [
        Commands\CustomMakeController::class
    ];

Upvotes: 1

Related Questions