For Codyas team members, express installation method is available. CD to the folder you want to locate the project and execute the following command, make sure to replace PROJECT_NAME for your actual project name:
curl -s https://gist.githubusercontent.com/leoantunez/0446c79dcc717de4fbbc0cb8214c26d9/raw | bash -s PROJECT_NAME
Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.
Open a command console, enter your project directory and execute:
composer require codyas/skeleton-bundle
The bundle provides a base class with common structure Codyas\Skeleton\Model\UserModel. The UserModel class
already assumes email as primary identification and includes the following fields, so you don't need to implement them:
If your user class requires another behaviour feel free to implement your own structure but make sure to implement the
UserModelInterface
Create an entity that will represent your users and extend the Codyas\Skeleton\Model\UserModel class.
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Codyas\Skeleton\Model\UserModel;
use Codyas\Skeleton\Model\UserModelInterface;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: UserRepository::class)]
class User extends UserModel implements UserModelInterface
{ }
Add any other fields in your class (if needed) and once done you are ready to sync the changes with your database.
Go to config/packages/security.yaml and adjust the file with the following changes
security:
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: app_user_provider
custom_authenticator: App\Security\EmailAuthenticator
#form_login:
# login_path: app_login
# check_path: app_login
# enable_csrf: true
logout:
path: app_logout
target: app_login
access_control:
- { path: ^/login, roles: PUBLIC_ACCESS }
- { path: ^/, roles: ROLE_ADMIN }
The App\Security\EmailAuthenticator class is automatically created when installing the bundle, but you can adjust it
to your needs or create a custom authenticator and modify the security configuration.
The default template is Tabler and integrated with
TablerBundler, so when installing this bundle you should also adjust the
config/packages/tabler.yaml file.
This bundle supports automated read and write operations via the CRUD manager. To allow the handler to operate entities in your application, you must ensure that you meet the following requirements:
getId(): ?int: Return the entity's identifier.renderDataTableRow(\Codyas\SkeletonBundle\Model\RowRendererArguments $arguments): array: This method must return an array, where each position relates with your entity columns.__toString(): string: An string representation of the entity.If it is necessary to export the data from the table, it is possible to configure the entity for this purpose, although it will always be necessary to implement the method that generates the file. To activate this behavior you must follow the following steps:
exportConfiguration key in the entity configuration attribute. This configuration parameter expects an
array of \Codyas\SkeletonBundle\Model\EntityExportDefinition where each element represents a format in which the information is exported.
You also need to specify the both the implementation class and method that will generate the report:
#[CrudEntity(
# (..)
exportConfiguration: [
new EntityExportDefinition(
format: 'csv',
label: 'CSV',
icon: 'fas fa-file-csv',
# Specify here the class that implements your report generation.
implementationClass: ReportsGeneratorService::class,
# Specify the class method that implements your report generation.
callbackMethod: 'generateCSV'
)
]
)]
<?php
namespace App\Services;
use App\Entity\User;
use Codyas\SkeletonBundle\Model\CrudEntityInterface;
use Doctrine\ORM\Query;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
#[AutoconfigureTag(name: CrudEntityInterface::EXPORT_IMPLEMENTATION_SERVICE_TAG)]
readonly class ReportsGeneratorService
{
public function __construct(private readonly PaginatorInterface $paginator)
{
}
public function generateUsersCSV(Query $query, Request $request): Response
{
$handle = fopen('php://temp', 'r+');
$page = 1;
$pagination = $this->paginator->paginate($query, $page, 25);
while ($pagination->getItems()) {
/** @var User $user */
foreach ($pagination->getItems() as $user) {
fputcsv($handle, $user->toArray());
}
$page++;
$pagination = $this->paginator->paginate($query, $page, 25);
}
rewind($handle);
$csvContent = stream_get_contents($handle);
fclose($handle);
$fileName = strtoupper(uniqid());
return new Response(
$csvContent,
200,
[
'Content-Type' => 'text/csv',
'Content-Disposition' => "attachment; filename=\"{$fileName}.csv\"",
]
);
}
}
How can I help you explore Laravel packages today?