PHP & Web Development Blogs

Showing 26 to 30 of 47 blog articles.
4568 views · 1 years ago

![Working With Thin Controller And Fat Model Concept In Laravel](https://images.ctfassets.net/vzl5fkwyme3u/18L41PfcrcYYkM0qAsCous/7caca26b8cfb5a643d8cb16b14ae5eae/AdobeStock_147870533.jpeg?w=1000)

Models and controllers are one of the most essential programming handlers in the Laravel MVC framework, and both are used vastly for different functional operations. Models in Laravel are created inside the app folder and are mostly used to interact with the database using Eloquent ORM, while the controllers are located inside the directory App/Http/Controllers.

As a programmer, you should have the knowledge how to keep the balance in between the programming usage of Models and controllers. As which one should be more utilized for allowing functional tasks in applications deployed on any [PHP MySQL hosting](https://www.cloudways.com/en/php-cloud-hosting.php).

## What is the Concept of Thin Controller and FAT Models

The concept of the thin controller and fat model is that we do less work in our controllers and more work in our models. Like we use our controllers to validate our data and then pass it to the models. While in models, we define our actual functional logic and main coding operations of the desired application. This code structuring process is also a very basic concept of MVC and also the differentiating factor from the conventional complex programming which we mistakenly ignore sometimes.

## Why FAT Controllers Are Bad For Handling Code

Controllers are always meant to be defined short and concise, and it should only be used for receiving requests and return responses to it. Anything else further should be programmed in Models, which is actually made for main functional operations.

Placing functional logic in controllers can be bad for many reasons for your applications deployed on any _hosting for PHP_. As it not only makes code structure long but also makes it complex sometimes. Further placing code in Controllers is also not recommended because if same functionality is needed somewhere else in route, then pulling out the whole code from their becomes difficult and so its reusability in the application.

Though Laravel is an MVC framework while developing on laravel, we sometimes ignore this and write mostly all our code including the extending of App\Model and all our functional logic in controller route methods. What we can do here is we can create a sub model of our parent model. For example, our parent model is User then we can create another sub model of username in CustomerModel if you are using the same User model for all types of users. In this model, we will write all the logic related to user type Customer.

So now let's take an example of my existing blog creating comment system with laravel and vuejs. In that article, you can see I have made so much mess in my controller methods. Mostly, I have written all my comments logic in my methods, so to shorten that let's clean them in this article. Inside app folder, I will create a new file with name CommentModel.php. Inside this file, I will write my whole logic for comment functions. This is my basic file:

```php

<?php

namespace App;

use App\Comment;

use App\CommentVote;

use App\CommentSpam;

use App\User;

use Auth;

class CommentModel

{

}

?>

```

Right now it contains no function but has the reference of all my models which I required for this model. Let's first add a function named **getallcomments** passing **$pageId** as a parameter inside it. The function will get all the comments for the given page:

```php

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;

/ / dd($photo->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');

}

```

Now I will create another function named **replies** which takes **$commentId** as a parameter. The function is more or less programmed in the same manner as the upper function get all comments.

```php

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');

}

```

Now lets create a function **create comment **which passes **$array** as a parameter in it:

```php

public function createComment($arary)

{

$comment = Comment::create($array);

if($comment)

return [ "status" => "true","commentId" => $comment->id ];

else

return [ "status" => "false" ];

}

```

Similarly, Now I will create all the function for comment in my **CommentModel**, so that all the functions gets accumulated in one model.

```php

<?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;

/ / dd($photo->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;

}

}

}

?>

```

Now we have all our required methods in **CommentModel**. So now let's clean up **CommentController** which is currently bit complex and lengthy in code structure. As right now **CommentController **look like this:

```php

<?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

{

/ **

* Get Comments for pageId

*

* @return Comments

*/

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;

/ / dd($photo->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');

}

/ **

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

public function store(Request $request)

{

$this->validate($request, [

'comment' => 'required',

'reply_id' => 'filled',

'page_id' => 'filled',

'users_id' => 'required',

]);

$comment = Comment::create($request->all());

/ / dd($comment);

if($comment)

return [ "status" => "true","commentId" => $comment->id ];

}

/ **

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param $commentId

* @param $type

* @return \Illuminate\Http\Response

*/

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";

}

}

/ **

* Remove the specified resource from storage.

*

* @param int $id

* @return \Illuminate\Http\Response

*/

public function destroy($id)

{

/ /

}

}?>

```

After cleaning up the controller it will look much simpler and easy to understand like this:

```php

<?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();

}

/ **

* Get Comments for pageId

*

* @return Comments

*/

public function index($pageId)

{

return $this->commentModel->getAllComments($pageId);

}

/ **

* Store a newly created resource in storage.

*

* @param \Illuminate\Http\Request $request

* @return \Illuminate\Http\Response

*/

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());

}

/ **

* Update the specified resource in storage.

*

* @param \Illuminate\Http\Request $request

* @param $commentId

* @param $type

* @return \Illuminate\Http\Response

*/

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());

}

}

}

?>

```

## Wrap Up!

So Isn't it looking much cleaner and simpler to understand now? This is what actually a thin controller and fat model looks like. We have all our logic related to Comment system programmed in our **CommentModel **and our controller is now just used to transfer data from the user to our model and returning the response which is coming from our model.

So this is how the structuring of the thin controller and fat model is made. Give your thoughts in the comments below.

2860 views · 1 years ago

![Custom extension to Laravel Application class](https://images.ctfassets.net/vzl5fkwyme3u/505z7itnLO8suUC6oGMmKq/fa48e59682497e2480e9f1822ec8a498/AdobeStock_184180246.jpeg?w=1000)

Hello folks! This post is for those of you using Laravel. This beautiful framework makes web development super-easy compared to most of competitors. In the heart of Laravel is the Application class, which is responsible for bootstrapping, registering services and also serves as a dependency injection container. What I do with my Laravel apps, is that I take a slight detour from the common path by adding a custom Application class. While this is not really necessary, I find this approach nice, and will try to share my thought below.

It's normal practice in Laravel world to build all kinds of objects like this:

```php

$cache = app("cache");

```

I find it a bit confusing to call `app("cache"")` and expect a `Cache\Repository` instance as result. If I pass the result of this call to a function that requires a `Cache\Repository` as parameter, I will probably have a code inspection warning from IDE. Moreover, if I want proper autocompletion, I will have to add additional comment:

```php

/ ** @var \Illuminate\Contracts\Cache\Repository */

$cache = app("cache");

```

This is where a custom application class might be handy:

```php

namespace App;

class MyApp extends Application

{

public function cacheRepository(): Repository

{

return $this->make(Repository::class);

}

}

```

This way I get a `TypeError` in case of a misconfiguration, and I have a type-hint which allows the IDE to recognize the return value. Bye-bye nasty comment lines and IDE warnings! I make a method per service, with type-hints, like `dbConnection()` or `viewFactory()` - works really well for me!

I also thought that, if I have a custom class, then all the custom setup that normally you have in `bootstrap/app.php`, should reside in that custom class:

```php

namespace App;

class MyApp extends Application

{

public function __construct()

{

define('LARAVEL_START', microtime(true));

define("APP_ROOT", realpath(__DIR__ . "/../"));

parent::__construct(APP_ROOT);

$this->setUp();

}

private function setUp()

{

/ / all the stuff from bootstrap/app.php, goes here

$this->singleton(

Contracts\Http\Kernel::class,

\App\Http\Kernel::class

);

}

}

```

Then your `bootstrap/app.php` becomes just this:

```php

return new \App\MyApp;

```

The Laravel `app()` function will also return an instance of MyApp from now on. However, it's @phpdoc says it returns `\Illuminate\Foundation\Application`, so for better clarity, I also added my own accessor method:

```php

namespace App;

class MyApp extends Application

{

public static function app(): self

{

/ ** @var self $ret */

$ret = parent::getInstance();

return $ret;

}

}

```

I tend to limit the use of global/static functions and methods, but sometimes it can be handy, and whenever I need an instance of MyApp, I just call `MyApp::app()`. The IDE wil be aware of the return type due to the type-hint, so I get everything I want for clean and clear development.

With your projects in Laravel, you may or may not want to follow this particular advice, but just be aware that extending a framework built-in classes for your team's comfort, is definitely something that can make your life easier. See you around, don't forget to leave comments!

12323 views · 1 years ago

![Generate PDF from HTML in Laravel 5.7](https://images.ctfassets.net/vzl5fkwyme3u/7BjL7ILSA84wgAUIo2K2yg/43ac850a592f4a272e910c30e06b97ba/AdobeStock_39841236.jpeg?w=1000)

Today, I will share with you how to create a PDF file from HTML blade file in Laravel 5.7. We will be using `dompdf` package for generating the PDF file.

In the below example, we will install `barryvdh/laravel-dompdf` using composer package and thereafter we will add new route url with controller. Then we will create a blade file. Then after we have to just run project with serve and we can check the PDF file is for download.

## Download Laravel 5.7

Now I am going to explain the step by step from scratch with laravel installation for `dompdf`. To get started, we need to download fresh Laravel 5.7 application using command, so open our terminal and run the below command in the command prompt:

```php

composer create-project --prefer-dist laravel/laravel blog

```

## Install laravel-dompdf Package

Now we will install barryvdh/laravel-dompdf composer package by using the following composer command in ourLlaravel 5.7 application.

```php

composer require barryvdh/laravel-dompdf

```

Then the package is successfully installed in our application, after that open config/app.php file and we need to add alias and service provider.

**config/app.php**

```php

'providers' => [

....

Barryvdh\DomPDF\ServiceProvider::class,

],

'aliases' => [

....

'PDF' => Barryvdh\DomPDF\Facade::class,

]

```

## Create Routes

Now we need to create routes for the items listing. so now open our "routes/web.php" file and we need to add following route.

**routes/web.php**

```php

Route::get('demo-generate-pdf','[email protected]');

```

## Create Controller

Here,we need to create a new controller *HomeController* (mostly it will be there, we can skip this step if we don't need to create a controller) that will manage our pdf generation using the `generatePDF()` method of route.

**app/Http/Controllers/HomeController.php**

```php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PDF;

class HomeController extends Controller

{

public function demoGeneratePDF()

{

$data = ['title' => 'Welcome to My Blog'];

$pdf = PDF::loadView('myPDF', $data);

return $pdf->download('demo.pdf');

}

}

```

## Create Blade File

In the final step, let us create demoPDF.blade.php in the resources/views/demoPDF.blade.php for structure of pdf file and add the following code:

**resources/views/demoPDF.blade.php**

```html

<!DOCTYPE html>

<html>

<head>

<title>Hi</title>

</head>

<body>

<h1>Welcome to My BLOG - {{ $title }}</h1>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod

tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse

cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non

proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

</body>

</html>

```

Now run the below command for serve and test it:

```curl

php artisan serve

```

3517 views · 1 years ago

![Press Release](https://images.ctfassets.net/vzl5fkwyme3u/6iJumo9OXCq0m0EqOuueAm/61e3dea3c18f9d770a6906099856e015/press_release.jpeg?w=1000)

To say that we have been hard at work here at Nomad PHP, or that I'm excited about these three announcements would be a tremendous understatement. Over the past several months, behind the scenes, we've been working to bring even more features and benefits to Nomad PHP - these have already included unlimited streaming of all past meetings and access to PHP Architect.

Available today, however, you'll also have access to online, live workshops - as well as soon have the ability to stream select PHP conferences live, and finally to prove the knowledge you have gained through our online certification.

## Online, Live Workshops

Like our online meetings, we are excited to announce that available today you can participate in online, live, and interactive workshops. Our [first workshop](https://beta.nomadphp.com/live/bC7lqFvjeouMoC4cqoaU6/Workshop--Achieving-Undisturbed-REST--Part-1-/) will feature Michael Stowe, author of Undisturbed REST: a guide to Designing the Perfect API as he demonstrates how to build the perfect API using modern technologies and techniques.

Additional workshops will be announced as we continue, with a minimum of one workshop per quarter. These workshops will be part of your Nomad PHP subscription, and will be recorded for later viewing.

## Nomad PHP Certification

With the many changes impacting the PHP ecosystem, we're proud to announce the ability to prove your knowledge with our online certification. Each certification is made up numerous, randomly selected questions to be completed within a specific time frame. Depending on the exam it may or may not be proctored, but all exams monitor user activity to ensure compliance.

To pass the exam, a passing grade (specified on each exam) must be completed for each section within the allotted time frame. Failure to complete or pass any section will result in a failing grade for the entire exam.

Upon completion, you will receive a digital certification with verification to post on LinkedIn or your website, as well as having your Nomad PHP updated to show the passed certification.

Initial certification exams will include PHP Developer Level I, PHP Engineer Level II, and API Specialist Level I. The PHP Developer exam will cover core components of PHP, the Engineer will cover a broad spectrum of topics including modern technologies, and the API Specialist will cover REST design and architecture practices.

All three exams will be available by January 31, 2019, and will be included with a Nomad PHP subscription.

## Stream Select PHP Conferences Live

One of the primary goals of Nomad PHP is to bring the community together, and allow users all over the country to participate in conference level talks. What better way to do this than to bring community conferences online?

Like our traditional talks, these conferences and select conference sessions will be live-streamed as part of your Nomad PHP subscription, allowing you to participate in real-time with in-person conference attendees.

The first conference to be streamed will be [DayCamp4Developers: Beyond Performance](https://daycamp4developers.com/) on January 18, 2018. Additional conferences to be streamed will be announced shortly.

## Community and Corporate Sponsorships

With these new additions to Nomad PHP, now is the perfect time to take advantage of our new [Community and Corporate sponsorships](/static/advertise).

Your support of Nomad PHP not only makes all the above possible, but allows Nomad PHP to continue to serve and give back the community. We're proud, that despite operating at a loss, to have already contributed over **$4,000** to the PHP community in the last 5 months.

To learn more about the sponsorship and community opportunities we have available, please visit our [Advertising section](/static/advertise).

### Other Ways to Support Nomad PHP

Of course, while financial support helps us keep afloat and do more for the community, there are even more, and just as important ways to support Nomad PHP. Please consider [linking to Nomad PHP](/static/webmasters), or [sharing the service](/invite) with your friends.

2649 views · 1 years ago

![Iterator in PHP](https://images.ctfassets.net/vzl5fkwyme3u/5I2KRmarVm4Gg0GuYcOeSk/b8c5156475053a19537059c19f244d61/AdobeStock_144145979.jpeg?w=1000)

Every time I see this

```php

$users = [new User(), new User()];

```

I see a lost opportunity to use Iterator.

### Why Iterators?

Collections are an awesome way to organize your previously no-named array. There is a couple of reasons why you should use iterators. One of reason stays for behavior, you can specify exact behavior on standard calls such as next, current, valid etc. Other reason could be that you want to ensure that collection contains an only specific type of an object.

Understand a suffer from using an array of unknown value types.

Very common in the PHP world arrays are used to store all kind of data, in many dimensions in many nested forms. Arrays introduced infinite flexibility to the developer, but because of that, they become very evil.

### Example:

- Your function (getUsers) returns an array of User objects.

- Another function (setUsersToActiveState) using getUsers output array and set all users active status to true.

- setUsersToActiveState loop through the array and expect to call a specific method on array item. For example, the method name is getActiveStatus.

- If given array is an array of desired objects which have a callable method getActiveStatus, all fine. But if not exception will be thrown.

- How we can ensure that given array is always an array of objects of a specific type?

```php

public function getUsers(): array

{

/ **

here happen something which gets users from database

....

**/

return $userArray;

}

public function setUsersToActiveState()

{

$users = $this->getUsers();

/ ** @var User $param */

foreach ($users as $user) {

if(!$user->getActiveStatus()) {

$user->setActiveStatus(true);

}

}

}

```

There immediately two problems occurred.

1. One is the problem of type. Our IDE doesn't know what's inside array of $users, so because of that IDE can't suggest us how to use $user element. (I put this comment block / ** @var User $param */ above foreach, it works for phpStorm and I guess for some other IDEs)

2. Your colleagues! How they possibly know what's inside array if there is no any hint.

3. Bonus problem, getUsers can return literally any array and there won't be warning in the system.

### Solution

```php

/ / Create a collection which accepts only Users

class UsersCollection implements \IteratorAggregate

{

/ ** @var array */

private $users = [];

public function getIterator() : UserIterator

{

return new UserIterator($this);

}

public function getUser($position)

{

if (isset($this->users[$position])) {

return $this->users[$position];

}

return null;

}

public function count() : int

{

return count($this->users);

}

public function addUser(User $users)

{

$this->users[] = $users;

}

}

/ / Create an Iterator for User

class UserIterator implements \Iterator

{

/ ** @var int */

private $position = 0;

/ ** @var UsersCollection */

private $userCollection;

public function __construct(UsersCollection $userCollection)

{

$this->userCollection = $userCollection;

}

public function current() : User

{

return $this->userCollection->getUser($this->position);

}

public function next()

{

$this->position++;

}

public function key() : int

{

return $this->position;

}

public function valid() : bool

{

return !is_null($this->userCollection->getUser($this->position));

}

public function rewind()

{

$this->position = 0;

}

}

```

### Tests

Off course there is the tests to ensure that our Collection and Iterator works like a charm. For this example I using syntax for PHPUnit framework.

```php

class UsersCollectionTest extends TestCase

{

/ **

* @covers UsersCollection

*/

public function testUsersCollectionShouldReturnNullForNotExistingUserPosition()

{

$usersCollection = new UsersCollection();

$this->assertEquals(null, $usersCollection->getUser(1));

}

/ **

* @covers UsersCollection

*/

public function testEmptyUsersCollection()

{

$usersCollection = new UsersCollection();

$this->assertEquals(new UserIterator($usersCollection), $usersCollection->getIterator());

$this->assertEquals(0, $usersCollection->count());

}

/ **

* @covers UsersCollection

*/

public function testUsersCollectionWithUserElements()

{

$usersCollection = new UsersCollection();

$usersCollection->addUser($this->getUserMock());

$usersCollection->addUser($this->getUserMock());

$this->assertEquals(new UserIterator($usersCollection), $usersCollection->getIterator());

$this->assertEquals($this->getUserMock(), $usersCollection->getUser(1));

$this->assertEquals(2, $usersCollection->count());

}

private function getUserMock()

{

/ / returns the mock of User class

}

}

class UserIteratorTest extends MockClass

{

/ **

* @covers UserIterator

*/

public function testCurrent()

{

$iterator = $this->getIterator();

$current = $iterator->current();

$this->assertEquals($this->getUserMock(), $current);

}

/ **

* @covers UserIterator

*/

public function testNext()

{

$iterator = $this->getIterator();

$iterator->next();

$this->assertEquals(1, $iterator->key());

}

/ **

* @covers UserIterator

*/

public function testKey()

{

$iterator = $this->getIterator();

$iterator->next();

$iterator->next();

$this->assertEquals(2, $iterator->key());

}

/ **

* @covers UserIterator

*/

public function testValidIfItemInvalid()

{

$iterator = $this->getIterator();

$iterator->next();

$iterator->next();

$iterator->next();

$this->assertEquals(false, $iterator->valid());

}

/ **

* @covers UserIterator

*/

public function testValidIfItemIsValid()

{

$iterator = $this->getIterator();

$iterator->next();

$this->assertEquals(true, $iterator->valid());

}

/ **

* @covers UserIterator

*/

public function testRewind()

{

$iterator = $this->getIterator();

$iterator->rewind();

$this->assertEquals(0, $iterator->key());

}

private function getIterator() : UserIterator

{

return new UserIterator($this->getCollection());

}

private function getCollection() : UsersCollection

{

$userItems[] = $this->getUserMock();

$userItems[] = $this->getUserMock();

$usersCollection = new UsersCollection();

foreach ($userItems as $user) {

$usersCollection->addUser($user);

}

return $usersCollection;

}

private function getUserMock()

{

/ / returns the mock of User class

}

}

```

### Usage

```php

public function getUsers(): UsersCollection

{

$userCollection = new UsersCollection();

/ **

here happen something which gets users from database

....

**/

foreach ($whatIGetFromDatabase as $user) {

$userCollection->addUser($user);

}

return $userCollection;

}

public fucntion setUsersToActiveState()

{

$users = $this->getUsers();

foreach ($users as $user) {

if(!$user->getActiveStatus()) {

$user->setActiveStatus(true);

}

}

}

```

As you can see setUsersToActiveState remains the same, we only do not need to specify for our IDE or collagues what type $users variable is.

### Extending functionalities

Believe or not you can reuse this two objects and just change names of variables to fit most of the needs. But if you want any more complex functionality, than feel free to add it in iterator or collection.

#### Example 1

For example, let's say that userCollection accepts only users with age more than 18. Implementation will happen in UsersCollection class in the method addUser.

```php

public function addUser(User $users)

{

if ($user->getAge() > 18) {

$this->users[] = $users;

}

}

```

#### Example 2

You need to add bulk users. Then you can expand your userCollection with additional method addUsers and it might look like this.

```php

public function addUsers(array $users)

{

foreach($users as $user) {

$this->addUser(User $users);

}

}

```

SPONSORS