Read the following code as if it were a documentation page:
$user = $request->user();
if ($user->cannot('place-order')) {
abort(403);
}
$validated = $request->validate([
'vendor_id' => ['required', Rule::exists('vendors', 'id')],
'products' => ['required', 'array'],
'products.*.id' => [
'required',
Rule::exists('products', 'id')->where('vendor_id', $request->vendor_id)
],
'products.*.quantity' => ['required', 'integer', 'min:1']
]);
$order = Order::create([...$validated, 'status' => Status::Pending]);
SendOrderToVendor::dispatch($order)->onQueue('orders');
NewOrderPlaced::dispatch($order);
return ['order' => $order];
This code verifies that the user is authenticated and authorized to place orders. It will also validate the request data, store the order in the database, dispatch jobs for background processing, broadcast an event to update the frontend in real time, and return the order in JSON format.
It can handle more than 500,000 orders per month when hosted on a $6/month server. The only additional fees are for a CDN (if you want your assets to be served faster) and a domain name. If you need more, add Swoole to the mix and get shared-memory capabilities & coroutines.
Your website is automatically protected against XSS, session hijacking, CSRF, SQL injection, host header attacks, and other vulnerabilities.
When you need to add extra functionality, you have access to a wealth of resources and documentation (both free and paid) to assist you. If you need extra hands, there are thousands, if not millions, of developers available for hire.
The community is one of the web's oldest. It's also one of the most welcoming and caring. It recognizes that the web development profession is a positive sum game and believes in open-source.
You can write code however you want; strict or dynamic typing, functional or OOP. And if you invest in writing tests, it won't matter how frequently you change it or how many people are working on it.
During development, you write the code and immediately run it. There is no need to compile anything. You can use any JS library or framework you want when creating a frontend. You can also use the built-in templating engine to render everything on the server side and add JS sprinkles for reactivity and animations.
In 2004, PHP 5 was released. You probably remember if you were on the internet at the time. You probably also lost interest in it soon after. It wasn't progressing quickly enough.
PHP 7 was released in 2015. This changed everything. PHP has become faster, more secure, and more enjoyable to use. More importantly, development became more active. PHP is now a technology that can keep up.
I'd love to have you try PHP if you're still with me. And the best place to begin is with the Laravel PHP framework.
When utilizing the full power of Laravel, the previous code may look like this:
class OrdersController extends Controller
{
public function store(StoreOrderRequest $request)
{
$order = $request->user()->orders()->create($request->validated());
SendOrderToVendor::dispatch($order);
NewOrderPlaced::dispatch($order);
return ['order' => $order];
}
}
If you want a deep dive into the code, read this article.
Also, check this overview of Laravel by Aaron Francis. Then, watch this series created by Jeffrey Way, one of the best teachers you'll meet online.
Don't be concerned about writing code at first. Just sit back and watch Jeffrey build a PHP app like you're watching Netflix.
After that, install PHP and create something for yourself. Perhaps it's your personal blog, a SaaS you've always wanted to create, or a personal finance tool. Make something you'd want to use.
Good luck!
I'm Mohamed Said. I work with companies and teams all over the world to build and scale web applications in the cloud. Find me on twitter @themsaid.