欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

laravel配置-全局变量

程序员文章站 2022-03-09 21:46:20
...

对于配置变量,想在全局范围都使用,一直没有找到一个好的解决方案。 之前引入自动加载文件时,在里面定义全局变量,都不生效!后来定义成常量,勉强将就着使用!

常量只支持标量,不支持变量、表达式等,导致了各种局限性。昨天摸索了下,发现在 config/ 下配置有效!可以使用!总结下来,希望对大家有帮助

1.新建:

	config/my.php

2.参照系统的配置文件,发现,该文件都返回一个数组

	<?php
		return [
		];

3.更进一步方便配置,我们可以组装一个数组变量,最后返回

	<?php
		$options = [
			'name' => 'dongxuemin',
			'age' => 30,
		];
		$options['sex'] = '男';
		if(env('APP_ENV') == 'test'){
			$options['env'] = 'test';
		}
	    $options['url']' = (php_sapi_name() == 'cli') ? '' : url('/test'),
		return $options;

4.再总结一个问题:
针对3中的 ‘url’ 的配置,在http内核访问,没有问题!
但是当我在 ‘console’ 中执行命令,各种报错!

	composer update | php artisan * 	// 各种命令都报了一个错!!!
	Fatal error: Uncaught ReflectionException: Class log does not exist in /private/var/www/pinxuejianyou/vendor/laravel/framework/src/Illuminate/Container/Container.php:734 8    


	这个问题可谓是各种坑啊!!!和 'log' 有什么关系!!找了好多,有一篇文章总结的很有道理!
	https://*.com/questions/34978828/uncaught-reflectionexception-class-log-does-not-exist-laravel-5-2
	复制作者发现问题:
		/*
		Okay, after many hours of digging, the solution for my problem has been found. The reason why I say my problem is because the Exception is very mis-leading.
		Uncaught ReflectionException: Class log does not exist
		This exception simply means Laravel tried to log an error but couldn't instantiate Laravel's Log class. This is not due to the Log class going walk-abouts or hiding. This is because Laravel is still going through its boot process & has yet to load the Log class.
		So, this exception is thrown because an error occurred during the boot cycle of Laravel - when this error occurred it tried to throw an exception - but it can't throw an exception because the Log class is yet the be loaded. Hence the reason we get a ReflectionException
		This has occurred in all versions of Laravel the only reason we have seen the exception thrown in laravel 5.1 <= is because previously Laravel silently discarded the problem & carried on through its boot process - basically, your app would still break however you would not receive the Log class exception.
		In my particular case I didn't have the php-mysql extension installed causing Laravel to break during its boot process.
		Ultimately, it is incredibly difficult to debug what you may have done wrong due to the error been very mis-leading.
		I hope this helps someone!
		*/
	和我的这个问题,并不一样,但是里面提到的 "本质原因" 应该正确!这个异常的原因是:
		laravel发现一个错误,并尝试使用 log 记录这个错误,但是还并未实例化log类。是因为,laravel还在启动过程中,还并未加载log类。
	是什么导致错误的!这个问题比较难找!(正如上面说的,作者自己都找了好几个小时!)我也是对比了这个修改的文件版本,回退到上个版本,没有问题!然后一个个文件排查!!
	尤其是:
		http访问下,没问题,就是 console下有问题!!
		最后,排查到是:url() 这个函数的问题!
		思考了下,console下,自然是不会引入 Request、URL这类扩展,所以 url() 这里导致了错误!
	再次搜索:
		laravel下,如何区分是在 http 还是 console 内核?
		http://laravel-recipes.com/recipes/2/checking-if-youre-running-in-the-console
		laravel是给我们提供了这个方法的 
			App::runningInConsole()
		其实就是使用PHP函数 
			php_sapi_name() 进行的判断!
		php高版本,估计已经统一成了 'cli',之前还有各种 apache2handler, cgi等等,可查看该函数详细了解!
	最终解决方案:
	    $options['url']' = (php_sapi_name() == 'cli') ? '' : url('/test'),
		

相关标签: 配置-全局变量