How to debug multiple dependant php files in a browser? - php

I am new to PHP and Laravel.
I am quite confused about debugging such application. I've set up idea IDE and xdebug, installed Laravel plugin, run apache server, etc.
Right now i have this line of code in my /App/MaterialDivider.php
$provision = \Config::get('constants.MARKET_PROVISION');
and this in my \config\constants.php
return [ 'MARKET_PROVISION' => 0.3 ];
When I run artisan commands in my vagrant homestead box, this way of calling constants works within my seeder file. The problem is that I don't know how to make Laravel work when I run single file in a browser.
Apart from that, a simple
dd($var)
doesn't work either.
When working with multiple files that contain dependant classes, is it necessary to include them? Or should using 'use' be enough? Or is editing composer.json (maybe autoload-dev section?) the right choice?
So far I've been programming in C(++), so this is all very new for me, please be patient.
Partialy [SOLVED]. To anyone having similar problems: namespaces are the thing, you might be looking for.

Related

Annoying intelisense warning in VS Code for Laravel's built in commands

The image above is self explanatory, I always get Undefined method [every built in laravel method] every time I create a Laravel project. I'm using PHP Intelephense extension to help me with autocompletion and stuffs.
This kind of annoying since they sometimes got mixed with another errors I've made in the project and make it difficult to find where was the real error is.
Also sometimes, I have to deep dive into the vendor, facades, illuminate, etc folders to let the IDE scans each files and knows that the class and methods is exist in the project to make the autocompletion works.
I'm using the latest version of VS Code and PHP Intelephense extension. Installing laravel-ide-helper does nothing. Is there anything I can do to fix this issue?

How does Laravel Artisan created php files with properly formatted lines

This is just a general question, how does Laravel Artisan able to create proper .php file e.g. make:controller with the correct formatting and line breaks?
Is there a good PHP script which can help one develop similar php codes.
Thanks
Ultimately, Artisan relies on Shell scripts included with Laravel. Which are written in the language of Shell and reside in files ending with the extension .sh. Here is a website that contains more explanation of what those are and how to develop them: https://www.shellscript.sh/ If this is ever unavailable simply searching "How to create a shell script" will get you the answer.
PHP can execute these scripts, for example, when you visit that PHP page in your browser, or you trigger it from the command line, assuming you have PHP installed on your server.
Artisan, is custom built to allow you to extend it with more commands, and there are tutorials available to show you how to do that, but it's a completely separate process from creating custom shell commands.
PHP files are just text files, you can easily create them with any language. Also in Laravel usually used so called stubs, templates for classes like migrations, controllers etc.
Just go and look how as Laravel does that under the hood.

Creating first PHP CLI, getting started

Hello I would like to create a simple PHP CLI to do some scaffolding. For a new project I am working on, we are using Magento to build an eCommerce store. Magento to comes with many default modules, but you can also create your own and we will have to be creating many custom modules.
I would like to create a simple PHP CLI which can be run to generate a default file path/structure for a module. It would be passed in something like the module name and maybe even path of the application and then generate the template folders and files.
I am new to PHP development, so i think this will be a simple enough project and a good learning experience. However I am having trouble knowing where to start. The end goal would probably be to have it archived in a .phar file that can just be run on the command line.
Any suggestions on how I can get started with this?
Thanks for any help, it is much appreciated.
No reason you can't. PHP in the command line reads the $argv array from the command line.
Here's one of my CLI apps; perhaps it can be instructive: https://github.com/dalecosp/NixArchive/blob/master/archive

The point of Yii2 environments folder

I am trying to work what the point of the environments folder is.
Originally I had the idea that you could point the webserver to the different dev and prod folders in the environment folder but after reading up a bit I realise this is not the case.
In Yii 1 you would solve this by just having multiple index.php's i.e.:
index.php
index-local.php
So the question is what benefit does this new environment structure actually give me over the old way?
I've found environments very useful in allowing me to keep a common code base for multiple client projects (based on Yii App Advanced) and setting up a different environment for each specific client, keeping their custom code private and separate.
To do this I store the environments folder in a separate git repo from the rest of the code and pull down the relevant folder on a client/project basis.
This lets me use a base common code for all projects and add/override any file for a specific client or project whilst still allowing separate dev/prod config settings. If the client uses other developers too, they are also catered for. In this way, only common code I choose will be shared amongst clients and custom code will be kept private.
I've also moved the composer.json file into the environments folder so I can pull in different extensions per client/project keeping those private too.
That init command can be a very powerful tool and you don't have to limit yourself to the template provided by the core developers.
If you don't need environments, then don't use them, but I assure you some people will find it very useful.
Yii2 documentation in WIP, but you should read this :
https://github.com/yiisoft/yii2/blob/master/docs/guide/apps-advanced.md#configuration-and-environments
You need to use yii init command to switch between these environments.
EDIT :
This new environment feature is more than just use different config file. You can use different folder structure, different entry script...etc
Personnaly I won't use this feature, I don't need it (I will use a different entry script as with Yii 1), but I think this is not useless.
I think you didn't get the real purpose of environments introduced in Yii2.
I'll try to explain what was the main purpose of adding environments to yii from the developers point of view on an example and hope you will really appreciate its usefulness.
Let's suppose for a moment that you are a team of developers (e.g. 5-7 person) working on mid-to-large project implemented in Yii. To effectively work on that project your team decides to use some CVS or SVN (e.g. GIT) and keep all the files of the project in repository in cloud for the whole team. That's de facto standard while working on mid-to-large projects in teams and nobody will resist that it's the only comfortable and easy way.
Ok, now let's suppose you use Yii 1.x or Yii2 with the approach of different entry scripts to differentiate between local (development) and production environments to connect to db or set some other environment specific configs. Everything is ok and working. But suppose your team members implemented something new on the project and you check out repository to work on updated version and you suddenly find out that your local config file (in this case entry script with config) is overwritten with other team member's file who pulled the changes to repository (because each of you is using your local machine db with other database name or OS, or config, or simply because your team uses one local development server db, but you are on vacation and can't use anything except your local machine).
So generally Yii2 environment adds more flexibility for using different environments each with it's own specific configurations while using also general (common) configs when working in teams on mid-to-large projects hence why the example in guide is given on advanced app project.
Surely you can overcome everything stated above with some solutions or .gitignore which is used by default to overcome the problem stated in Yii2 with environments. But:
Why bother if everything is already done?
and
It was just one little example of usefulness of Yii2 environments. More depends on the project and your imagination.
Overall Yii2 is great product. Not only it adds many new features to already great framework, but it also is more robust and flexible than Yii 1.x (despite the fact that Yii 1.x was already very robust).
As for Laravel or any other PHP framework, it really depends... Everyone will find his/her own favorite.
For those who are tired of copying files around, I created a useful script that you can run in background to keep the files in sync on your dev environment:
File sync-env-files.sh
#!/bin/bash
ENVIRONMENT_DIR="/var/www/example.com/environments/dev/"
DIR="/var/www/example.com/"
while [ true ]; do
for envFile in `find $ENVIRONMENT_DIR -type f`
do
file=${envFile/$ENVIRONMENT_DIR/$DIR}
if [ `stat -c "%Y" $file` -gt `stat -c "%Y" $envFile` ]; then
#echo "copying modified file $file to $envFile"
/bin/cp -f $file $envFile
fi
done
sleep 2
done
Then run the script in background or add to cron with flock
nohup server/sync-env-files.sh >/dev/null 2>&1 &
I would like to mention in addition to #AngelCoding, since this question still gets seen, that I use the environments folder lots now and definitely see the point of it.
The very first things I do in any open source project is create one project for the code base on GitHub and then another, private, one on Bitbucket for the configuration, in other words the environments folder.
Having this folder has made it a lot easier for me to separate my configuration into a private repository.
So the environments folder has a lot of uses and really helps to separate configuration for easier usage even if it does not seem like it initially.

FuelPHP's Autoloader is failing to find classes

I'm having a problem with FuelPHP failing to autoload classes. On my staging server (Ubuntu, PHP 5.3.10) it is unable to find custom classes in the fuel/app/classes directory, and it also can't run oil test (I get the error message sh: 1: phpunit: not found). Oddly, it works fine on my local development version (Windows, PHP 5.3.6).
I suspected it might have something to do with Composer, which I'm using for the first time on this project, but the problem is not fixed when I comment out the line require APPPATH.'vendor/autoload.php'; from bootstrap.php (the app still fails to load custom classes from fuel/app/classes)
I'm stumped: I've used FuelPHP on lots of projects and have never had any problems with the Autoloader. What's particularly puzzling is that the same code seems to work fine in one place and not in another. I'd be very grateful for any suggestions about how to fix this.
I realize this question was asked a long time ago, but I had the same problem, so for the benefit of anyone else with this problem, here is what worked for me:
create a new php file called oil.php in the app/config directory with the following code:
<?php
// Unit tests will get shell error 'phpunit: command not found' unless
// the path to php unit is specified.
return array (
'phpunit' => array (
'binary_path' => 'fuel/vendor/bin/phpunit',
),
);
I am using fuel 1.7.2. More information can be found here.
Although the above code fixed the specific PHPunit problems, I still had issues with Fuel and autoloaders not working. Fuel PHP does not follow psr-4 (many of the core fuel files have multiple class definitions in the same file), which can cause problems with certain autoloaders.

Categories