Skip to content

TIP

AIM Admin provides an abstract controller that acts as the bootstrapper for its functionalities. You need to extend this class in your controllers to access the available features of AIM Admin.

AbstractAimAdminController

php
use CodeCoz\AimAdmin\Controller\AbstractAimAdminController;

The AbstractAimAdminController is the core class of AIM Admin. It initializes and provides various functionalities needed for the admin panel.

By extending AbstractAimAdminController, you gain access to all the built-in features of AIM Admin, making it easier to manage your admin panel. Let's dive into each one by one.

Let's inspect a simple Aim Admin Controller

php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;

use App\Contracts\Services\ProductServiceInterface;
use App\Contracts\Repositories\ProductRepositoryInterface;
use App\Http\Requests\ProductRequest;

use CodeCoz\AimAdmin\Controller\AbstractAimAdminController;  

use CodeCoz\AimAdmin\Field\ButtonField; 
use CodeCoz\AimAdmin\Field\FileField;  
use CodeCoz\AimAdmin\Field\TextareaField;  
use CodeCoz\AimAdmin\Field\IdField;  
use CodeCoz\AimAdmin\Field\TextField;  

use CodeCoz\AimAdmin\Helpers\Helper;  

class ProductController extends AbstractAimAdminController
{

    public function __construct(private readonly ProductRepositoryInterface $repo, private readonly ProductServiceInterface $productService)  
    {
    }

    public function getRepository()  
    {
        return $this->repo;
    }


    public function configureActions(): iterable
    {
        return [
            ButtonField::init(ButtonField::DETAIL)->linkToRoute('product.show'),  
            ButtonField::init(ButtonField::EDIT)->linkToRoute('product.edit'),  
            ButtonField::init(ButtonField::DELETE)->linkToRoute('product.delete'),  
            ButtonField::init('new', 'new')->linkToRoute('product.create')->createAsCrudBoardAction(),  
            ButtonField::init('submit')->createAsFormSubmitAction(),  
            ButtonField::init('cancel')->linkToRoute('product.list')->createAsFormAction(),  
            ButtonField::init('back')->linkToRoute('product.list')->createAsShowAction()->setIcon('fa-arrow-left'),  
        ];

    }

    public function configureForm()   
    {
        $route = Helper::getAttribute('route');
        $fields = [ 
            IdField::init('id'),
            TextField::init('name'),
            TextField::init('product_code'),
            TextField::init('product_price'),
            FileField::init('product_image'),
            TextareaField::init('product_description'),
            TextField::init('product_quantity')->setInputType('number'),
        ];  
        $this->getForm($fields)  
            ->setName('form_name')  
            ->setMethod('post')  
            ->setActionUrl(route($route));  
    }

    public function configureFilter(): void
    {
        $fields = [
            TextField::init('name'),   
            // TextField::init('other')
        ];
        $this->getFilter($fields);   
    }

    public function list()
    {
        $this->initGrid(['product_code', 'product_name', 'product_price', 'product_quantity'], pagination: 10); 
        return view('aim-admin::list');
    }

    public function create()
    {
        Helper::setAttribute('route', 'product.create');
        $this->initCreate(); 
        return view('aim-admin::create');
    }

    public function store(ProductRequest $request): RedirectResponse
    {
        //Store Your Data
        $validated = $request->validated();
        $this->productService->store($validated);
    }

    public function show($id)
    {
        $this->initShow($id, ['id', 'created_at']); 
        return view('aim-admin::show');
    }

    public function edit($id)
    {
        Helper::setAttribute('route', 'product.update');
        $this->initEdit($id); 
        return view('aim-admin::edit');
    }

    public function delete($id)
    {
        // Set your delete functionality
    }

}

In the example above, the __construct method uses dependency injection to include a repository and a service. These dependencies are necessary for generating lists, showing items, and editing items within the admin panel.

php
public function getRepository() 
{
    return $this->repo;
}

Get the Repository

The getRepository function is used to resolve and return the repository instance. This function provides an easy way to access the repository within the controller of your application.

Configuring the Form Action

php
   public function configureActions(): iterable
    {
        return [
            ButtonField::init(ButtonField::DETAIL)->linkToRoute('product.show'), 
            ButtonField::init(ButtonField::EDIT)->linkToRoute('product.edit'), 
            ButtonField::init(ButtonField::DELETE)->linkToRoute('product.delete'), 
            ButtonField::init('new', 'new')->linkToRoute('product.create')->createAsCrudBoardAction(), 
            ButtonField::init('submit')->createAsFormSubmitAction(), 
            ButtonField::init('cancel')->linkToRoute('product.list')->createAsFormAction(), 
            ButtonField::init('back')->linkToRoute('product.list')->createAsShowAction()->setIcon('fa-arrow-left'), 
        ];

    }

ConfigureActions

The configureActions method is used to define different routes for items used in CRUD generation, such as create, show, edit, and delete. It is also used to customize the submit button of the create form, the cancel button, the new button, and the back button of the CRUD form.

ButtonField::DETAIL

ButtonField::init(ButtonField::DETAIL): Defines the button & route for showing the item details.

ButtonField::EDIT

ButtonField::init(ButtonField::EDIT): Defines the button & route for editing the item.

ButtonField::DELETE

ButtonField::init(ButtonField::DELETE): Defines the button & route for deleting the item.

New

ButtonField::init('new', 'new'): Defines the button & route for creating a new item and adds it as a CRUD board action.

Submit

ButtonField::init('submit'): Customizes the submit button of the create form.

Cancel

ButtonField::init('cancel'): Customizes the cancel button of the form and links it to the back route.

Back

ButtonField::init('back'): Customizes the back button of the form, linking it to a specific link and setting an icon.

createAsCrudBoardAction()

This method configures the button to appear in the top right corner of the table, typically used for actions like creating a new item.

createAsFormAction()

This method configures the button to appear on forms, such as submit or cancel buttons on a create form.

createAsShowAction()

This method configures the button to appear in the item view, providing actions related to the specific item being viewed.

createAsFilterSubmitAction()

This method configures the button to appear in the filter form submit, providing actions related to the specific item being searched.

createAsFilterAction()

This method configures the button to appear in the filter form reset, providing actions related to reset the filter form.

linkToRoute()

This method sets the route for the button, defining the URL to which the button will link.

Configuring the Form

php
public function configureForm()  
{
    $route = Helper::getAttribute('route');
    $fields = [ 
        IdField::init('id'),
        TextField::init('name'),
        TextField::init('product_code'),
        TextField::init('product_price'),
        FileField::init('product_image'),
        TextareaField::init('product_description'),
        TextField::init('product_quantity')->setInputType('number'),
    ]; 
    $this->getForm($fields) 
        ->setName('form_name') 
        ->setMethod('post') 
        ->setActionUrl(route($route)); 
}

This method is used to generate a form, specify the form action route, set the form name, and define the form method type.

$route

$route = Helper::getAttribute('route'): This line retrieves the route attribute using a helper function. The route is used to define the action URL of the form.

$fields

$fields: This array defines the fields that will be included in the form. Each field is initialized with its respective type:

Fields

IdField::init('id'): Initializes an ID field.

TextField

TextField::init('product_price'): Initializes a text field for the product price.

FileField

FileField::init('product_image'): Initializes a file field for the product image.

TextareaField

TextareaField::init('product_description'): Initializes a textarea field for the product description.

getForm

$this->getForm($fields): This method initializes the form with the specified fields.

setName

setName('form_name'): Sets the name of the form.

setMethod

setMethod('post'): Sets the HTTP method for the form to 'POST'.

setActionUrl

setActionUrl(route($route)): Sets the action URL for the form, using the route retrieved earlier.

Configuring the Filter

php
public function configureFilter(): void
{
    $fields = [
        TextField::init('name'),  
        // TextField::init('other')
    ];
    $this->getFilter($fields);  
}

This method is used to add filters to the items table and specify which fields you want to filter.

$fields

$fields: This array defines the fields that will be included in the filter. Each field is initialized with its respective type:

INFO

TextField::init('name') Initializes a text field filter for the 'name' attribute as $this->getFilter($fields) This method initializes the filter with the specified fields, allowing users to filter the items table based on these criteria.

This method sets up the filters for the items table, enabling you to define which fields you want to filter. In this example, the filter is set up for the 'name' field, but you can add more fields as needed.

Generating the List Grid

php
public function list()
{
    $this->initGrid(['product_code', 'product_name', 'product_price', 'product_quantity'], pagination: 10);
    return view('aim-admin::list');
}

This method is used to generate a built-in table as a grid and allows the user to specify which columns should be visible.

php
$this->initGrid(['product_code', 'product_name', 'product_price', 'product_quantity'], pagination: 10)

This line initializes the grid with the specified columns and sets the pagination limit.

php
['product_code', 'product_name', 'product_price', 'product_quantity']:

An array that specifies the columns to be displayed in the grid. pagination: 10: Sets the pagination limit to 10 items per page.

php
return view('aim-admin::list')

This line returns the view for the list grid, rendering it with the specified configuration.

Creating a New Item

php
public function create()
{
    Helper::setAttribute('route', 'product.create');
    $this->initCreate();
    return view('aim-admin::create');
}

This method bootstraps the form generator and returns the view for creating a new item.

php
Helper::setAttribute('route', 'product.create')

This line sets the attribute 'route' to 'product.create' using a helper function. This route will be used for form submission.

php
$this->initCreate()

This method initializes the form creation process. It sets up the necessary configurations for the form generator.

php
return view('aim-admin::create')

This line returns the view for the create form, rendering it with the specified configurations.

Store the Item

php
public function store(ProductRequest $request): RedirectResponse
{
    //Store Your Data
    $validated = $request->validated();
    $this->productService->store($validated);
}

This is a store method generally used in laravel project.

Showing an Item

php
public function show($id)
{
    $this->initShow($id, ['id', 'created_at']);
    return view('aim-admin::show');
}

This method is used to display the details of a specific item, showing only the specified columns.

php
$this->initShow($id, ['id', 'created_at'])

This line initializes the item view for the specified item ID, displaying only the specified columns. $id: The ID of the item to be displayed.

php
['id', 'created_at']:

An array specifying the columns to be visible in the item view.

php
return view('aim-admin::show');

This line returns the view for displaying the item's details, rendering it with the specified configuration.

This method sets up the item view for displaying the details of a specific item, allowing you to define which columns should be visible in the view.

Editing an Item

php
public function edit($id)
{
    Helper::setAttribute('route', 'product.update');
    $this->initEdit($id);
    return view('aim-admin::edit');
}

This method is used to populate an item in the form for editing purposes, with specific columns.

php
Helper::setAttribute('route', 'product.update');

This line sets the attribute 'route' to 'product.update' using a helper function. This route will be used for form submission when updating the item.

php
$this->initEdit($id);

This method initializes the form with the item's data for editing. It loads the specified item by its ID and prepares the form with the item's current values.

php
return view('aim-admin::edit')

This line returns the view for the edit form, rendering it with the specified configurations.

This method sets up the form for editing an item, defining the route for form edit submission and initializing the form with the item's current data before returning the view for the edit form.

setFields() Method

Purpose

The setFields method allows dynamic modification of form fields within the context of editing an existing record. It can update existing fields, replace fields with new types, or add new fields to the form.

Usage Examples

php
use CodeCoz\AimAdmin\Field\TextField;
use CodeCoz\AimAdmin\Field\ChoiceField;

public function edit($id)
{
    $this->initEdit($id)->setFields([
        'username' => function(TextField $field) {
            return $field->setLabel('New Username Label');
        },
        'age' => TextField::init('age', 'User Age')->setInputType('number'),
        'for_id[]' => function (ChoiceField $field) {
            return $field->setDefault([1, 5]);
        },
    ]);

    return view('aim-admin::edit');
}

Updating an Existing Field

php
$this->initEdit($id)->setFields([
    'username' => function($field) {
        return $field->setLabel('New Username Label');
    }
]);

Adding a New Field

php
use CodeCoz\AimAdmin\Field\EmailField;

$this->initEdit($id)->setFields([
    'email' => EmailField::init('email', 'Email Address')
                         ->setRequired(true)
]);

Notes

  • Always use $this->initEdit($id)->setFields(...) to ensure the form is properly initialized for editing.
  • Ensure that update functions for existing fields return the modified field.
  • New fields must be instances of FieldInterface.
  • This method allows for flexible form field manipulation when editing an existing record.
  • The initEdit method likely sets up the form with the existing record's data, which setFields then modifies.

Developed By ❤️ Taki Elias