Learn from your fellow PHP developers with our PHP blogs, or help share the knowledge you've gained by writing your own.
$ composer require monolog/monolog
$ composer require monolog/monolog:1.18.0
$ composer require monolog/monolog:>1.18.0
$ composer require monolog/monolog:~1.18.0
$ composer require monolog/monolog:^1.18.0
$ composer global require "phpunit/phpunit:^5.3.*"
$ composer update
$ composer update monolog/monolog
4: Don’t install dev dependencies
In a lot of projects I am working on, I want to make sure that the libraries I download and install are working before I start working with them. To this end, many packages will include things like Unit Tests and documentation. This way I can run the unit Tests on my own to validate the package first. This is all fine and good, except when I don’t want them. There are times when I know the package well enough, or have used it enough, to not have to bother with any of that.5: Optimize your autoload
Regardless of whether you --prefer-dist or --prefer-source, when your package is incorporated into your project with require, it just adds it to the end of your autoloader. This isn’t always the best solution. Therefore Composer gives us the option to optimize the autoloader with the --optimize switch. Optimizing your autoloader converts your entire autoloader into classmaps. Instead of the autoloader having to use file_exists() to locate a file, Composer creates an array of file locations for each class. This can speed up your application by as much as 30%.$ composer dump-autoload --optimize
$ composer require monolog/monolog:~1.18.0 -o
<?php
namespace App;
use App\Comment;
use App\CommentVote;
use App\CommentSpam;
use App\User;
use Auth;
class CommentModel
{
}
?>
public function getAllComments($pageId)
{
$comments = Comment::where('page_id',$pageId)->get();
$commentsData = [];
foreach ($comments as $key) {
$user = User::find($key->users_id);
$name = $user->name;
$replies = $this->replies($key->id);
$photo = $user->first()->photo_url;
$reply = 0;
$vote = 0;
$voteStatus = 0;
$spam = 0;
if(Auth::user()){
$voteByUser = CommentVote::where('comment_id',$key->id)->where('user_id',Auth::user()->id)->first();
$spamComment = CommentSpam::where('comment_id',$key->id)->where('user_id',Auth::user()->id)->first();
if($voteByUser){
$vote = 1;
$voteStatus = $voteByUser->vote;
}
if($spamComment){
$spam = 1;
}
}
if(sizeof($replies) > 0){
$reply = 1;
}
if(!$spam){
array_push($commentsData,[
"name" => $name,
"photo_url" => (string)$photo,
"commentid" => $key->id,
"comment" => $key->comment,
"votes" => $key->votes,
"reply" => $reply,
"votedByUser" =>$vote,
"vote" =>$voteStatus,
"spam" => $spam,
"replies" => $replies,
"date" => $key->created_at->toDateTimeString()
]);
}
}
$collection = collect($commentsData);
return $collection->sortBy('votes');
}
protected function replies($commentId)
{
$comments = Comment::where('reply_id',$commentId)->get();
$replies = [];
foreach ($comments as $key) {
$user = User::find($key->users_id);
$name = $user->name;
$photo = $user->first()->photo_url;
$vote = 0;
$voteStatus = 0;
$spam = 0;
if(Auth::user()){
$voteByUser = CommentVote::where('comment_id',$key->id)->where('user_id',Auth::user()->id)->first();
$spamComment = CommentSpam::where('comment_id',$key->id)->where('user_id',Auth::user()->id)->first();
if($voteByUser){
$vote = 1;
$voteStatus = $voteByUser->vote;
}
if($spamComment){
$spam = 1;
}
}
if(!$spam){
array_push($replies,[
"name" => $name,
"photo_url" => $photo,
"commentid" => $key->id,
"comment" => $key->comment,
"votes" => $key->votes,
"votedByUser" => $vote,
"vote" => $voteStatus,
"spam" => $spam,
"date" => $key->created_at->toDateTimeString()
]);
}
}
$collection = collect($replies);
return $collection->sortBy('votes');
}
public function createComment($arary)
{
$comment = Comment::create($array);
if($comment)
return [ "status" => "true","commentId" => $comment->id ];
else
return [ "status" => "false" ];
}
<?php
namespace App;
use App\Comment;
use App\CommentSpam;
use App\CommentVote;
use App\User;
use Auth;
class CommentModel
{
public function getAllComments($pageId)
{
$comments = Comment::where('page_id', $pageId)->get();
$commentsData = [];
foreach ($comments as $key) {
$user = User::find($key->users_id);
$name = $user->name;
$replies = $this->replies($key->id);
$photo = $user->first()->photo_url;
$reply = 0;
$vote = 0;
$voteStatus = 0;
$spam = 0;
if (Auth::user()) {
$voteByUser = CommentVote::where('comment_id', $key->id)->where('user_id', Auth::user()->id)->first();
$spamComment = CommentSpam::where('comment_id', $key->id)->where('user_id', Auth::user()->id)->first();
if ($voteByUser) {
$vote = 1;
$voteStatus = $voteByUser->vote;
}
if ($spamComment) {
$spam = 1;
}
}
if (sizeof($replies) > 0) {
$reply = 1;
}
if (!$spam) {
array_push($commentsData, [
"name" => $name,
"photo_url" => (string) $photo,
"commentid" => $key->id,
"comment" => $key->comment,
"votes" => $key->votes,
"reply" => $reply,
"votedByUser" => $vote,
"vote" => $voteStatus,
"spam" => $spam,
"replies" => $replies,
"date" => $key->created_at->toDateTimeString(),
]);
}
}
$collection = collect($commentsData);
return $collection->sortBy('votes');
}
protected function replies($commentId)
{
$comments = Comment::where('reply_id', $commentId)->get();
$replies = [];
foreach ($comments as $key) {
$user = User::find($key->users_id);
$name = $user->name;
$photo = $user->first()->photo_url;
$vote = 0;
$voteStatus = 0;
$spam = 0;
if (Auth::user()) {
$voteByUser = CommentVote::where('comment_id', $key->id)->where('user_id', Auth::user()->id)->first();
$spamComment = CommentSpam::where('comment_id', $key->id)->where('user_id', Auth::user()->id)->first();
if ($voteByUser) {
$vote = 1;
$voteStatus = $voteByUser->vote;
}
if ($spamComment) {
$spam = 1;
}
}
if (!$spam) {
array_push($replies, [
"name" => $name,
"photo_url" => $photo,
"commentid" => $key->id,
"comment" => $key->comment,
"votes" => $key->votes,
"votedByUser" => $vote,
"vote" => $voteStatus,
"spam" => $spam,
"date" => $key->created_at->toDateTimeString(),
]);
}
}
$collection = collect($replies);
return $collection->sortBy('votes');
}
public function createComment($arary)
{
$comment = Comment::create($array);
if ($comment) {
return ["status" => "true", "commentId" => $comment->id];
} else {
return ["status" => "false"];
}
}
public function voteComment($commentId, $array)
{
$comments = Comment::find($commentId);
$data = [
"comment_id" => $commentId,
'vote' => $array->vote,
'user_id' => $array->users_id,
];
if ($array->vote == "up") {
$comment = $comments->first();
$vote = $comment->votes;
$vote++;
$comments->votes = $vote;
$comments->save();
}
if ($array->vote == "down") {
$comment = $comments->first();
$vote = $comment->votes;
$vote--;
$comments->votes = $vote;
$comments->save();
}
if (CommentVote::create($data)) {
return true;
}
}
public function spamComment($commentId, $array)
{
$comments = Comment::find($commentId);
$comment = $comments->first();
$spam = $comment->spam;
$spam++;
$comments->spam = $spam;
$comments->save();
$data = [
"comment_id" => $commentId,
'user_id' => $array->users_id,
];
if (CommentSpam::create($data)) {
return true;
}
}
}
?>
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Comment;
use App\CommentVote;
use App\CommentSpam;
use App\User;
use Auth;
class CommentController extends Controller
{
public function index($pageId)
{
$comments = Comment::where('page_id',$pageId)->get();
$commentsData = [];
foreach ($comments as $key) {
$user = User::find($key->users_id);
$name = $user->name;
$replies = $this->replies($key->id);
$photo = $user->first()->photo_url;
$reply = 0;
$vote = 0;
$voteStatus = 0;
$spam = 0;
if(Auth::user()){
$voteByUser = CommentVote::where('comment_id',$key->id)->where('user_id',Auth::user()->id)->first();
$spamComment = CommentSpam::where('comment_id',$key->id)->where('user_id',Auth::user()->id)->first();
if($voteByUser){
$vote = 1;
$voteStatus = $voteByUser->vote;
}
if($spamComment){
$spam = 1;
}
}
if(sizeof($replies) > 0){
$reply = 1;
}
if(!$spam){
array_push($commentsData,[
"name" => $name,
"photo_url" => (string)$photo,
"commentid" => $key->id,
"comment" => $key->comment,
"votes" => $key->votes,
"reply" => $reply,
"votedByUser" =>$vote,
"vote" =>$voteStatus,
"spam" => $spam,
"replies" => $replies,
"date" => $key->created_at->toDateTimeString()
]);
}
}
$collection = collect($commentsData);
return $collection->sortBy('votes');
}
protected function replies($commentId)
{
$comments = Comment::where('reply_id',$commentId)->get();
$replies = [];
foreach ($comments as $key) {
$user = User::find($key->users_id);
$name = $user->name;
$photo = $user->first()->photo_url;
$vote = 0;
$voteStatus = 0;
$spam = 0;
if(Auth::user()){
$voteByUser = CommentVote::where('comment_id',$key->id)->where('user_id',Auth::user()->id)->first();
$spamComment = CommentSpam::where('comment_id',$key->id)->where('user_id',Auth::user()->id)->first();
if($voteByUser){
$vote = 1;
$voteStatus = $voteByUser->vote;
}
if($spamComment){
$spam = 1;
}
}
if(!$spam){
array_push($replies,[
"name" => $name,
"photo_url" => $photo,
"commentid" => $key->id,
"comment" => $key->comment,
"votes" => $key->votes,
"votedByUser" => $vote,
"vote" => $voteStatus,
"spam" => $spam,
"date" => $key->created_at->toDateTimeString()
]);
}
}
$collection = collect($replies);
return $collection->sortBy('votes');
}
public function store(Request $request)
{
$this->validate($request, [
'comment' => 'required',
'reply_id' => 'filled',
'page_id' => 'filled',
'users_id' => 'required',
]);
$comment = Comment::create($request->all());
if($comment)
return [ "status" => "true","commentId" => $comment->id ];
}
public function update(Request $request, $commentId,$type)
{
if($type == "vote"){
$this->validate($request, [
'vote' => 'required',
'users_id' => 'required',
]);
$comments = Comment::find($commentId);
$data = [
"comment_id" => $commentId,
'vote' => $request->vote,
'user_id' => $request->users_id,
];
if($request->vote == "up"){
$comment = $comments->first();
$vote = $comment->votes;
$vote++;
$comments->votes = $vote;
$comments->save();
}
if($request->vote == "down"){
$comment = $comments->first();
$vote = $comment->votes;
$vote--;
$comments->votes = $vote;
$comments->save();
}
if(CommentVote::create($data))
return "true";
}
if($type == "spam"){
$this->validate($request, [
'users_id' => 'required',
]);
$comments = Comment::find($commentId);
$comment = $comments->first();
$spam = $comment->spam;
$spam++;
$comments->spam = $spam;
$comments->save();
$data = [
"comment_id" => $commentId,
'user_id' => $request->users_id,
];
if(CommentSpam::create($data))
return "true";
}
}
public function destroy($id)
{
}
}?>
<?php
namespace App\Http\Controllers;
use App\CommentModel;
use Illuminate\Http\Request;
class CommentController extends Controller
{
private $commentModel = null;
private function __construct()
{
$this->commentModel = new CommentModel();
}
public function index($pageId)
{
return $this->commentModel->getAllComments($pageId);
}
public function store(Request $request)
{
$this->validate($request, [
'comment' => 'required',
'reply_id' => 'filled',
'page_id' => 'filled',
'users_id' => 'required',
]);
return $this->commentModel->createComment($request->all());
}
public function update(Request $request, $commentId, $type)
{
if ($type == "vote") {
$this->validate($request, [
'vote' => 'required',
'users_id' => 'required',
]);
return $this->commentModel->voteComment($commentId, $request->all());
}
if ($type == "spam") {
$this->validate($request, [
'users_id' => 'required',
]);
return $this->commentModel->spamComment($commentId, $request->all());
}
}
}
?>
prometheus/client_php
and php-prometheus/client
./metrics
) where Prometheus can scrape the metrics using the Prometheus exposition format.prometheus.yml
) to include the target endpoint and define any additional scraping parameters.prometheus/client_php
library:require 'vendor/autoload.php';
use Prometheus\CollectorRegistry;
use Prometheus\Storage\APC;
use Prometheus\RenderTextFormat;
$registry = new CollectorRegistry(new APC());
$requestDuration = $registry->registerCounter('php_requests_total', 'Total number of PHP requests');
$requestDuration->inc();
$renderer = new RenderTextFormat();
echo $renderer->render($registry->getMetricFamilySamples());
php_requests_total
) to track the total number of PHP requests. We then increment this metric for each request and expose the metrics endpoint using the Prometheus exposition format.
class User {
public function getUserById($userId) {
}
public function updateUser($userId, $userData) {
}
}
<!DOCTYPE html>
<html>
<head> <title>User Profile</title>
</head>
<body> <h1>Welcome, <?php echo $user['username']; ?>!</h1> <p>Email: <?php echo $user['email']; ?></p>
</body>
</html>
class UserController {
public function profile($userId) {
$userModel = new User();
$userData = $userModel->getUserById($userId);
include 'views/profile.php';
}
}