Skip to content
On this page

Base Controller

A BaseController is a class in Laravel that acts as a foundation or blueprint for other controllers in the application. It provides a convenient way to define common functionality that can be shared across multiple controllers, such as CRUD operations and error handling.

A BaseController class can define methods that are mostly common to all controllers, such as methods for handling CRUD Operations, and these methods can be overridden in other controllers to provide custom behavior.

By using a BaseController, developers can reduce the amount of code duplication and make it easier to maintain their application. Additionally, it can help enforce best practices and improve code organization by centralizing common functionality.

In our application, we have created a BaseController named ResourceController inside app/Http/Controllers folder which handles basic CRUD Operations along with Breadcrumbs, redirections and error handling.

Some functions

index

  • The index method of a ResourceController is typically used to display a list of resources, such as a list of posts, articles, or products, to the user. It is usually the default method that is executed when a user visits the index page for a particular resource.
    public function index(Request $request, $id = '')
    {
        try {
            $data = $this->service->indexPageData($request);
            $data['breadcrumbs'] = $this->breadcrumbForIndex();
            $data['fullIndexUrl'] = URL::to($this->indexUrl());
            $this->setModuleId($id);
            return $this->renderView('index', $data);
        } catch (Throwable $throwable) {
            return redirect()->back()->withErrors(['alert-danger' => 'Server error.'])->withInput($request->all());
        }
    }

create

  • The create method of a ResourceController is typically used to display a form that allows the user to create a new resource, such as a new post, article, or product. It is usually executed when a user clicks on a "Create" button or visits a "Create" page.
    public function create()
    {
        $request = $this->defaultRequest();
        $request = app()->make($request);
        try {
            $data = $this->service->createPageData($request);
            $this->setModuleId($request->id);
            $data['breadcrumbs'] = $this->breadcrumbForForm('Create');
            return $this->renderView('form', $data);
        } catch (Throwable $throwable) {
            return redirect($this->getUrl())->withErrors(['alert-danger' => 'Server error.']);
        }
    }

store

The store method of a Laravel controller is typically used to persist data to the database that was submitted by a form. It is usually executed after the user submits the form, either by clicking a "Save" or "Submit" button.

    public function store()
    {
        if (!empty($this->storeValidationRequest())) {
            $request = $this->storeValidationRequest();
        } else {
            $request = $this->defaultRequest();
        }
        $request = app()->make($request);
        try {
            $response = $this->service->store($request);
            $this->setModuleId($request->id);

            if (@$response['msgError'] && !empty($response['msgError'])) {
                return redirect($this->getUrl())->withErrors(['alert-danger' => $response['msgError']]);
            } else if (@$response['redirectBackMsgError'] && !empty($response['redirectBackMsgError'])) {
                return redirect()->back()->withInput($request->all())->withErrors(['alert-danger' => $response['redirectBackMsgError']]);
            } else {
                return redirect($this->getUrl())->withErrors(['success' => 'Successfully created.']);
            }
        } catch (Throwable $throwable) {
            return redirect($this->getUrl())->withErrors(['alert-danger' => $throwable->getMessage() ?? 'Server error.']);
        }
    }