Twill is an open-source CMS toolkit designed specifically for Laravel to help developers rapidly create a customized and powerful admin interface. Integrating Twill's admin UI into an existing Laravel controller involves several steps, including installation, configuration, module creation, and extension within your application. This process allows you to leverage Twill's rich features like content management, media handling, and a block editor, while maintaining full control over your Laravel app's structure.
Installing Twill in an Existing Laravel Application
1. Add Twill to Your Laravel Project
Begin by requiring Twill through Composer in your existing Laravel app:
bash
composer require area17/twill:"^3.4"
This command installs Twill and prepares it for setup.
2. Run the Twill Installer Artisan Command
After installing the package, run:
bash
php artisan twill:install
This command publishes Twill assets, configuration files, routes, and setup. The installer also runs necessary database migrations creating tables required by Twill's default functionality.
3. Configure Storage Link
Twill uses Laravel's storage for media and files. Run:
bash
php artisan storage:link
This command creates a symbolic link from `public/storage` to `storage/app/public` for accessing uploaded files publicly.
4. Compile Frontend Assets
Twill uses Laravel Mix for asset management. Install dependencies and compile assets by running:
bash
npm install
npm run dev
This builds the CSS and JavaScript files Twill requires for its admin interface.
5. Serve the Application
Run your Laravel server:
bash
php artisan serve
Navigate to `/admin` in your browser to access the Twill admin panel.
Understanding Twill's Routing and Admin UI
Twill's admin interface is typically served from the `/admin` path by default, but this can be changed by setting environment variables such as `ADMIN_APP_PATH`. After running the installation, Twill registers its routes in a dedicated routes file (`routes/twill.php`), which you can customize according to your needs.
Twill's UI is a Vue.js SPA (Single Page Application) loaded inside Laravel views. It means the admin interface is autonomous but works seamlessly with Laravel's backend through APIs.
Creating a Twill Module
To integrate Twill's admin UI functionality into your Laravel app, you create one or more Twill modules. A module corresponds to a content type or resource you want to manageâsuch as pages, posts, projects, etc.
1. Generate Module Scaffolding
Twill provides an Artisan command to generate a module scaffold:
bash
php artisan twill:module name
Replace `name` with the content type you wish to manage, for example, `Page`.
2. Files Generated per Module
This generates:
- An Eloquent model (e.g., `Page.php`)
- A module repository (handling data queries)
- A controller extending Twill's base controller
- Migration files for the database schema
- Vue components for the admin interface
3. Migrate Database
Run migrations to create tables for your module:
bash
php artisan migrate
Integrating Twill Module into Laravel Controller
Twill modules come with their own controllers that extend `A17\Twill\Http\Controllers\Admin\AdminController`. To integrate Twill's admin UI into an existing Laravel controller structure, you generally follow these approaches:
- Use Twill's Module Controller:
The module's controller handles all admin tasks like listing, editing, deleting, and form processing. For example, `PageController` handles page content management. You typically don't have to merge this with existing Laravel controllers but instead, route admin tasks to these specialized controllers.
- Customize or Extend Module Controllers:
If you want to embed or integrate Twill-admin-related logic inside your existing Laravel controllers, you can extend or use Twill's controller traits and methods. For example, you can:
- Use Twill's repository patterns to fetch/manage content within your existing controllers.
- Render or redirect to Twill admin routes from your controllers to leverage the UI.
- Embedding Twill UI in Existing Routes:
If you want to serve Twill's UI inside existing routes, consider the following:
- Register Twill routes in `routes/twill.php`.
- Use Laravel middleware to gate admin routes.
- Redirect or link existing controllers to Twill admin URLs for module management.
Defining Data Forms and Admin UI Behavior
Each Twill module controller should define a `getForm` method that returns a `A17\Twill\Services\Forms\Form` instance. This form defines the fields shown in the admin UI for creating/editing records.
For example:
php
use A17\Twill\Services\Forms\Form;
use A17\Twill\Services\Forms\Fields\Text;
public function getForm(): Form
{
return Form::make()
->add(Text::make()->name('title')->title('Title')->translatable())
->add(Text::make()->name('slug')->title('Slug'));
}
This declarative form builder allows you to customize the input fields, validation, and layout in the admin UI.
Extending Admin UI with Media, Blocks, and Relations
Twill provides many features to enhance your admin UI:
- Media Library Integration: Attach images and files to modules using Twill's media library traits.
- Block Editor: Use modular rich text blocks to build complex page content layouts.
- Reordering & Bulk Editing: Enable manual reordering and bulk updates through the UI.
- Relationships: Define relationships between your models and use Twill's interface to manage them.
All these features are toggled and configured in the module controller by enabling traits and adding UI components to your forms.
Authentication and Access Control
Twill integrates with Laravel's authentication. Admin users are authenticated through Twill's own user management system or your existing Laravel auth setup.
By default, admin screens require logging in. You can configure guards, middleware, and roles by modifying `config/twill.php` and extending user models. This makes sure the admin UI is secure and accessible only to authorized users.
Customizing Twill Admin UI
You can customize the look and feel and extend the UI by:
- Publishing Vue components to your Laravel resources and modifying them.
- Adding custom routes and views in `routes/twill.php`.
- Using Twill's extensible design system and UI components, leveraging Vue.js.
Workflow Summary to Integrate Twill Admin UI
1. Install Twill package and run `twill:install`.
2. Set up database, storage links, and compile assets.
3. Run `twill:module` to generate modules for your content types.
4. Migrate your database.
5. Customize the module controller's `getForm` method to define admin forms.
6. Use the module controllers within your Laravel app by routing admin traffic to Twill.
7. Extend with media, block editor, and relations as needed.
8. Secure admin areas with authentication and role-based access.
9. Customize Vue.js components and routes to tailor the admin UI.
10. Use Twill's API and repository patterns in your existing Laravel controllers for deeper integration if needed.
Twill essentially acts as a fully featured admin panel platform for your Laravel app but is highly customizable and works smoothly alongside your existing Laravel codebase, making it easy to integrate the admin UI into your development workflow.
This integration approach allows you to effectively manage your application's backend content via Twill's admin UI, while still handling front-end or other app logic within your existing Laravel controllers. Twill's modular design, Laravel-native structure, and Vue-based UI provide a powerful toolkit for modern content management embedded seamlessly into Laravel applications.