Using require-dev with composer and laravel - laravel

I was using require-dev for my development tools dependencies. Like the laravel debugbar, the barryvdh ide-helper, etc...
When i get to my production server and run "composer update --no-dev --no-scripts" everything seems to be OK.
Then, you realize that you must remove your "dev providers" from the app.php array.
So whats the point of using require-dev? there isnt a "providers-dev" array?
UPDATE:
I was that i get it fixed but it's not working.
I create the file app/config/local/app.php with this:
<?php
return array(
'providers' => append_config(array(
'Barryvdh\Debugbar\ServiceProvider',
'Way\Generators\GeneratorsServiceProvider',
'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider',
'Barryvdh\Debugbar\ServiceProvider',
))
);
and on the top of app/start/global.php
$env = $app->detectEnvironment(function(){
$hosts = array(
'localhost' => 'local',
'127.0.0.1' => 'local',
);
if(isset($hosts[$_SERVER['SERVER_NAME']])){
return $hosts[$_SERVER['SERVER_NAME']];
}
});
I tried echoing the $env variable and it returns 'local' so it's working. When i open my site i can't see the debugbar but everything else is working.
Any tip?

Just add your dev providers to app/config/local/app.php and use append_config:
'providers' => append_config(array(
'Barryvdh\Debugbar\ServiceProvider',
))

Related

How to configure Laravel to use PhpRedis?

PHP 7.3
Laravel 5.8
Until now I was using Predis for my cache in the Laravel project. Now I want to switch to PhpRedis. I've read it's really simple (just config changes), but I have a lot of problems. I don't know what to begin with, so I'll write all what I know.
My hosting provider claims that PhpRedis is enabled.
The code below executed in a controller (Predis is set) works fine - I receive the set value.
$redis = new \Redis();
$redis->connect( 'socket path', 0 );
$redis->set('test', 'testValue');
print_r( $redis->get('test') );
However, the same code in the raw PHP file executed via SSH returns "Uncaught Error: Class 'Redis' not found in..."
Let's go to changes in config/database.php. Here is my configuration:
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'/*'phpredis'*/),
'cluster' => true,
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'/*'redis'*/),
'prefix' => Str::slug(env('APP_NAME'), '_').'_',
'parameters' => ['password' => env('REDIS_PASSWORD', null)],
],
'default' => [
'scheme' => 'unix',
'path' => env('REDIS_HOST'),
'host' => env('REDIS_HOST'),
'password' => env('REDIS_PASSWORD'),
'port' => env('REDIS_PORT'),
'database' => env('REDIS_CACHE_DB', 0)
],
(...) // other
],
When I change values to these in comments, my website shows just a blank page - any errors in the mailbox.
Furthermore, when I run for example "php73 artisan config:clear" in the SSH, console returns "Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension." in the Illuminate/Redis/Connectors/PhpRedisConnector.php.
When I change the alias in config/app.php from "Redis" to "RedisManager" and try again it returns
Uncaught Error: Class 'Redis' not found in /path/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:70.
What's going on? How to set Laravel's configuration to use PhpRedis? Maybe it's my hosting provider issue? Thanks in advance for every advice.
If I missed some important code, give me a sign - I will add it.
The PHPRedis libraries are not installed by default in a shared hosting environment, and are generally not part of a PHP installation by default. You would have to ask your host to install these libraries within their shared hosting platform.

Send Mail Using Lumen

Mail.php
return [
'driver' =>'smtp',
'host' => 'smtp.gmail.com',
//'port' => 587,
'port' =>465,
//'encryption' =>'tls',
'encryption' =>'ssl',
'username' => 'xxxxxxxx#gmail.com',
'password' => 'xxxxxxx',
// 'sendmail' => '/usr/sbin/sendmail -bs',
'sendmail' => '/usr/sbin/sendmail -t',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
Controller
$data = []; // Empty array
Mail::send('email.credentials', $data, function($message)
{
$message->to('xxxxxx#gmail.com', 'Jon Doe')->subject('Welcome!');
});
Error
Swift_TransportException Connection could not be established with host
smtp.gmail.com [A connection attempt failed because the connected
party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.
I Tried...
Change ssl / tls
Change the ports
Add "guzzlehttp/guzzle": "~5.3|~6.0" in composer.json
Add a new line in StreamBuffer.php
$options = array_merge($options, array('ssl' => array('verify_peer' =>
false,'verify_peer_name' => false,'allow_self_signed' => true )));
Please help .
Thank you.
1. Require illuminate/mail
Make sure you’re using the same version as your underlying framework (i.e. if you’re on Lumen v. 5.3, use composer require illuminate/mail "5.3.*").
composer require illuminate/mail "5.5.*"
2. Set up Lumen bootstrap/app.php
First, open your bootstrap.php and uncomment the following lines:
$app->withFacades();
$app->register(App\Providers\AppServiceProvider::class);
Also, add the following line below the last line you uncommented:
$app->configure('services');
This will allow you to define a ‘services’ config file and setup your mail service. Now I know that normally configuration is done in the .env file with Lumen, and we’ll use that shortly, but first we’ll need to write a small config file to map to the .env file.
3. Create your configuration files
Create a new folder at the root level of your install called config(if it doesn’t already exist). In the config folder, create two new files, one named services.php and another named **mail.php**.
In the services.php file paste the following:
<?php
return [
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
];
Lastly, add the following to your .env file:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=<your-mailgun-domain>
MAILGUN_SECRET=<your-mailgun-api-key>
Make sure you replace those sneaky placeholders with your actual key and domain. If you’re not using Mailgun, you can always use the other mail providers Mail comes with; have a look at the docs if you plan on using a different provider, they are all easy to set up once you’re at this point.
4. Send Email!
To send an email, use one of the following in your classes (depending on your preference):
use Illuminate\Support\Facades\Mail;
$data = []; // Empty array
Mail::send('email.credentials', $data, function($message)
{
$message->to('xxxxxx#gmail.com', 'Jon Doe')->subject('Welcome!');
});
Finally, don’t forget to read the Laravel Mail docs for more info on how to use this great library.
Have you turned on 2 layers security in your Google account (email address you config in .env file) which uses to send email.

Laravel 4.1 Deployment - Production .env.php not being recognised

For some reason, my production laravel app thinks that it is in the local environment.
/var/www/appname/.env.php
<?php
return
[
'APP_ENV' => 'production',
'DB_HOST' => 'HIDDEN',
'DB_NAME' => 'HIDDEN',
'DB_PASSWORD' => 'HIDDEN'
];
/var/www/appname/bootstrap/start.php
$env = $app->detectEnvironment(function()
{
return getenv('APP_ENV') ?: 'local';
});
/var/www/appname/app/config/database.php
...
...
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'lar_',
),
...
...
sudo php artisan env (via SSH)
`Current application environment: local
php artisan tinker then getenv('DB_NAME')
$ php artisan tinker
[1] > getenv('DB_NAME');
// false
So either my environment variables are not being set, or Laravel is not recognising my .env.php file for the production environment.
Update
With some help from Anultro on IRC, it appears that .env.php is not loaded yet. As such APP_ENV must be set before laravel tries to detect environments. This makes sense, because Laravel needs to know what environment is running before determining whether to use .env.php or .env.local.php.
Having said this, .env.php should still be used to store db credentials and secret keys etc... But I am still having a problem because the app is still returning false when I try to run getenv('DB_NAME')
Any suggestions?
For all who want to know... to solve this issue, I just edited my httpd.conf file on the production server as follows:
SetEnv APP_ENV production
Now laravel knows that the app is in production.
If you are using nginx which I have now migrated my site to add the following where you pass scripts to the FCGI server in the active sites-available /etc/nginx/sites-available/{sitename}:
fastcgi_param APP_ENV production;

How to setup autoload for Laravel 4 package

I have a new Laravel 4 app and am trying to add the laravel-oauth2 vendor package.
I ran
composer require taylorotwell/laravel-oauth2
composer dump-autoload
successfully. But I get Class 'OAuth2' not found
Advice much appreciated.
The standard method of adding packages to the Laravel 4 framework would be:
add a line to the "require" section of the main composer.json file, e.g.
"taylorotwell/laravel-oauth2": "0.2.*"
run "composer update"
This will download the package and refresh the autoload classes. I just did a test and it seems to work ok, remember to use the namespace:
$provider = \OAuth2\OAuth2::provider('facebook', array(
'id' => 'client id',
'secret' => 'client secret',
));
or
use OAuth2\OAuth2;
$provider = OAuth2::provider('facebook', array(
'id' => 'client id',
'secret' => 'client secret',
));

Using Eloquent ORM from Laravel 4 outside of Laravel

I created a folder 'eloquent' to start testing/learning the component, and my composer.json file is:
{
"require": {
"php": ">=5.3.0",
"illuminate/database": "4.0.*"
}
}
Below is my test.php file, with credentials removed. It works great, until I add ->remember(10) into the command. I'd like to look into adding the Illuminate Cache next then, if that's what's needed to start using ->remember(). Is anyone aware of any blog posts or tutorials on doing something like this?
<?php
/**
* Testing Laravel's Eloquent ORM
* #see https://github.com/illuminate/database
* #see http://laravel.com/docs/database
*/
require 'vendor/autoload.php';
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection(array(
'driver' => '',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
));
//$capsule->bootEloquent();
$capsule->setAsGlobal();
$name = Capsule::table('user')->where('id', 123 )->remember(10)->get();
var_dump( $name );
// PHP Fatal error: Uncaught exception 'ReflectionException' with message 'Class cache does not exist'
I'm not sure what the next step is to get ->remember() working. I tried adding illuminate/cache to the composer.json file and updated. I wasn't sure how to use it with Eloquent, outside of Laravel.
saff33r is right, but just to help someone like me that need "file" cache this is how to:
in your composer.json
"illuminate/cache": "4.0.*",
"illuminate/filesystem": "4.0.*",
in your boot file:
use \Illuminate\Database\Capsule\Manager as Capsule;
use \Illuminate\Cache\CacheManager as CacheManager;
use \Illuminate\Filesystem\Filesystem as Filesystem;
...
$container = $capsule->getContainer();
$container['config']['cache.driver'] = 'file';
$container['config']['cache.path'] = __DIR__ . '/uploads/cache/eloquent';
$container['config']['cache.connection'] = null;
$container['files'] = new Filesystem();
$cacheManager = new CacheManager($container);
$capsule->setCacheManager($cacheManager);
$capsule->bootEloquent();
It should already be pulling in "illuminate/cache", look in vendor and you should see it there.
You need to setup the cache manager, then pass that through by calling
$capsule->setCacheManager(CacheManager $cache);
I've not looked under the hood for details on how to do this but hopefully this will be enough details to get you going forward.
Edit:
Here's what you need to add to get it working:
use Illuminate\Cache\CacheManager as CacheManager;
$container = $capsule->getContainer();
$container->offsetGet('config')->offsetSet('cache.driver', 'array');
$cacheManager = new CacheManager($container);
$capsule->setCacheManager($cacheManager);
Obviously feel free to change the Cache Driver used but keep in mind that changing the Cache Driver will require adding the extra required settings.
Check out https://github.com/Luracast/Laravel-Database it provides full eloquent support outside laravel including artisan migrations and more
4.2 branch contains laravel 4.2.* components
5.2 branch contains laravel 5.2.* components
Disclosure: I'm the author of the above repository

Resources