1. Go to this page and download the library: Download cuongnd88/atom library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cuongnd88 / atom example snippets
use Atom\Http\Server;
try {
$server = new Server(['env']);
$server->handle();
} catch (Exception $e) {
echo $e->getMessage();
}
use Atom\Http\Request;
use Atom\Http\Response;
use Atom\Validation\Validator;
class UserController
{
use Validator;
public function index(Request $request)
{
$users = (new User)->get();
view('users.index', ['users' => $users]);
}
public function show(Request $request)
{
$id = $request->id;
$user = (new User)->find($id);
view('users.show', ['user' => $user]);
}
public function store(Request $request)
{
static::execute($request->all(), [
'email' => '
public function update(Request $request)
{
// Access parameters as properties
$name = $request->name;
// Or as array
$email = $request['email'];
// Get all parameters
$all = $request->all();
// Get HTTP headers
$headers = $request->headers();
$contentType = $request->headers('Content-Type');
// Access HTTP method and URI
$method = $request->method; // GET, POST, PUT, PATCH, DELETE
$uri = $request->uri;
}
use Atom\Db\Database;
$db = new Database();
// Select all
$users = $db->table('users')->get();
// Select specific columns
$users = $db->table('users')->select(['name', 'email'])->get();
// First record
$user = $db->table('users')->where(['id', 1])->first();
// With limit and offset
$users = $db->table('users')->limit(10)->offset(20)->get();
// Equality
$db->table('users')->where(['status', 'active'])->get();
// With operator
$db->table('users')->where(['age', '>', 18])->get();
// Multiple conditions (AND)
$db->table('users')->where([
['status', 'active'],
['age', '>', 18],
])->get();
// OR condition
$db->table('users')->where(['status', 'active'])->orWhere(['role', 'admin'])->get();
// WHERE IN
$db->table('users')->whereIn('id', [1, 2, 3])->get();
// WHERE NOT IN
$db->table('users')->whereNotIn('status', ['banned', 'inactive'])->get();
// WHERE BETWEEN
$db->table('users')->whereBetween('age', [18, 65])->get();
// WHERE NOT BETWEEN
$db->table('users')->whereNotBetween('score', [0, 50])->get();
// WHERE NULL / NOT NULL
$db->table('users')->whereNull('deleted_at')->get();
$db->table('users')->whereNotNull('email_verified_at')->get();
use Atom\Models\Model;
class User extends Model
{
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
}
$user = new User;
// Find by ID (returns model instance)
$user = $user->find(1);
// Find multiple by IDs (returns array)
$users = $user->find([1, 2, 3]);
// Create (inserts and returns model with ID)
$user = $user->create([
'name' => 'John',
'email' => '[email protected]',
]);
// Save (insert or update on duplicate key)
$user->save();
// Destroy by ID
$user->destroy(1);
// Destroy multiple
$user->destroy([1, 2, 3]);
// Convert to array
$data = $user->toArray();
$activeUsers = (new User)->where(['status', 'active'])->orderBy('name', 'ASC')->get();
use Atom\Validation\Validator;
class UserController
{
use Validator;
public function store(Request $request)
{
static::execute($request->all(), [
'name' => ' ]);
$errors = static::errors();
if (!empty($errors)) {
return Response::toJson(['errors' => $errors], 422);
}
// Validation passed
}
}
static::execute($input, $rules, [
' email.',
'between' => 'The %s must be between %s and %s.',
'integer' => 'The %s must be an integer.',
'string' => 'The %s must be a string.',
'in_array' => 'The %s has an invalid value.',
'max' => 'The %s must not exceed %s.',
'min' => 'The %s must be at least %s.',
'array' => 'The %s must be an array.',
'date' => 'The %s must be a valid date.',
'image' => 'The %s must be an image.',
'after' => 'The %s must be after %s.',
'before' => 'The %s must be before %s.',
'
use Atom\Guard\Auth;
class AuthController
{
use Auth;
public function login(Request $request)
{
// Authenticates and redirects or returns JWT token
static::login([
'email' => $request->email,
'password' => $request->password,
]);
}
public function dashboard()
{
// Verify authentication (redirects to fail URL if invalid)
static::check();
// Get current user
$user = static::user();
view('dashboard', ['user' => $user]);
}
}
use Atom\Http\Url;
$url = new Url();
// Permanent signed URL
$signedUrl = $url->signedUrl('/verify-email', ['user' => 42]);
// Temporary signed URL (expires in 3600 seconds)
$tempUrl = $url->temporarySignedUrl('/reset-password', 3600, ['token' => 'abc']);
// Verify signature
$valid = $url->identifySignature(); // Returns true/false
use Atom\File\CSV;
$csv = new CSV();
// Read CSV to array
$data = $csv->toArray($file, ['name', 'email', 'age']);
// Save and download CSV
$csv->save('export.csv', $data, true); // true =
use Atom\File\Image;
$image = new Image();
// Upload image
$image->upload('avatars', $request->files['photo']);
// Upload and resize
$image->uploadResize('thumbnails', $request->files['photo'], [200, 200]);
// Get EXIF metadata
$metadata = $image->getMetadata($request->files['photo']);
// Delete image
$image->delete('avatars/photo.jpg');
use Atom\File\Log;
$log = new Log();
$log->error('Something went wrong');
$log->info('User logged in');
$log->debug('Variable value: ' . $value);
use Atom\Container\Container;
$container = new Container();
// Register a binding
$container->set('App\Services\MailService');
// Resolve with automatic dependency injection
$service = $container->get('App\Services\MailService');
class OrderController
{
public function index(Request $request, PaymentService $payment)
{
// Both $request and $payment are automatically resolved
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.