What does isLocal() actually do in Laravel? - laravel

I've been looking into Laravel Telescope and it mentioned the ability of being able to run only in one's local environment as opposed to production by including the following code snippet in AppServiceProvider.
public function register()
{
if ($this->app->isLocal()) {
$this->app->register(TelescopeServiceProvider::class);
}
}
This works fine but I am trying to figure out what exactly the isLocal() method does. So far, I have not been able to find much information.
Thanks,

In your .env file you would have a APP_ENV set to local on your development environment and it will be different on other servers. So isLocal just checks if that is set to local or not.
Here is the answer from the repository.
To check for a different env other than local or production use this helper function:
config('app.env') // get the env
for production there is a isProduction() helper function on the app instance.

Related

Laravel forcing Http for asssets

this is a little bit strange because most of the questions here wanted to force https.
While learning AWS elastic beanstalk. I am hosting a laravel site there. Everything is fine, except that none of my javascripts and css files are being loaded.
If have referenced them in the blade view as :
<script src="{{asset('assets/backend/plugins/jquery/jquery.min.js')}}"></script>
First thing I tried was looking into the file/folder permissions in the root of my project by SSHing into EC2 instance. Didn't work even when I set the permission to public folder to 777.
Later I found out that, the site's main page url was http while all the assets url were 'https'.
I dont want to get into the SSL certificates things just yet, if it is possible.
Is there anyway I can have my assets url be forced to Http only?
Please forgive my naiveity. Any help would be appreciated.
This usually happens if your site is for example behind an reverse proxy, As the URL helper facade, trusts on your local instance that is beyond the proxy, and might not use SSL. Which can be misleading/wrong.
Which is probaly the case on a EC2 instance... as the SSL termination is beyond load balancers/HA Proxies.
i usually add the following to my AppServiceProvider.php
public function boot()
{
if (Str::startsWith(config('app.url'), 'https')) {
\URL::forceScheme('https');
} else {
\URL::forceScheme('http');
}
}
Of course this needs to ensure you've set app.url / APP_URL, if you are not using that, you can just get rid of the if statement. But is a little less elegant, and disallows you to develop on non https

CODEIGNITER How CI could read model name even if it is different

I have a 'Event_Model'. I used it on a controller. I called it as
$this->load->model('Event_model','eventM')
I wrote the code with phpstorm on Window but it is running on a linux VM.
It worked on a local server. I got an error when I had uploaded on a linux production server though.
Why did it happen?
I don't understand how my local server could read it
use COMMON\Models\Event\Event_Comm_Model as Common_Event_Model;
class Event_Model extends Common_Event_Model {
public function __construct() {
parent::__construct();
}
}
The model is like this.
Linux servers and hosted sites are very sensitive to file names. Make sure you respect the case sensitivity.
This can work on your local machine
$this->load->model('Event_model','eventM')
but for a hosted server use instead
$this->load->model('Event_Model','eventM')

Laravel - Disable SSL in local environment

I have implemented SSL in dev, test and prod environments.
I had to change some blade function to secure_asset() and secure_url for preventing Chrome to block content.
But know I have many issues in my local environment due to the changes I explained before.
Which is the way to work with SSL on server environments but not in local?
Regards
Add the follwing snipped to the register() method of one of your service providers and use the asset() helper instead of the secure_asset(). This way URLs will be forced to have the https scheme on environments which are not local but local environments will keep working with the unsecure http scheme.
public function register()
{
if (config('app.env') !== 'local') {
\URL::forceScheme('https');
}
}
function generate_asset_url($string)
{
if (in_array(env('APP_ENV'), ['local', 'dev'])) {
return asset($string);
}
return secure_asset($string);
}

Laravel route works with subdomain in 2 environments

I'm coding a platform to generate sub-websites.
I have a route like this who works very well in local :
//Website
Route::domain('{slug}.domain.test')->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
I want to be able to make it works as well in production (other domain), so i did :
//Website
Route::domain('{slug}.{domain}')->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
And in my template :
Website
The generated URL looks amazing, but the routing doesn't work and bring me to the parent page of the main domain.
What i am doing wrong ?
Thanks
Working with domain routes like this is a little bit of a pain in Laravel.
In an application recently, I parsed the domain part from the application URL and then set it as a configuration value like this:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
config([
'app.domain' => parse_url(config('app.url'), PHP_URL_HOST),
]);
}
}
You can then use that in your domain routes:
Route::domain('{slug}.'.config('app.domain'), function () {
// Subdomain routes that work in all environments
});
If you are using laravel homestead, you'll need to register every subdomain on the etc/hosts file and on Homestead.yaml, as you probably did with the main domain. Now, I'll recommend the structure:
app.test
subdomain1.app.test
subdomain2.app.test
I wouldn't recommend to use something like:
subdomain1.app1.test
subdomain2.app2.test
I mean, you could, but it doesn't make much sense, and you would also have to register all of this on your Homestead/Local and Production environments.
For the approach that I suggest, you could set up this on an environment variable. https://laravel.com/docs/6.x/configuration#environment-variable-types
You can add any env variable you want/need to the .env file, there's an APP_URL variable, but this includes the http protocol, you could add something like:
APP_DOMAIN=app.test
And for production
APP_DOMAIN=yourapp.com
Then on the routes file access it with the helper method env, you can omit the second parameter, or use it to setup a default value, in case you forget to put it on the .env file.
Route::domain('{slug}.' . env('APP_DOMAIN', 'default.com'))->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
BTW, this might be of help for setting up your slug value with named routes:
Setting up named routes within a subdomain group in Laravel 5.7

Magento Custom Controller working on local, throwing 500 error on staging server

I am having quite a strange problem. I have just pushed my local build to our staging server. I have a custom module with custom controllers which, when accessed locally, work perfectly fine. However, when I attempt to access ONE SPECIFIC controller on staging, it throws a 500 server error. All of the other controllers I wrote work except for this custom IndexController.php I wrote. Again, everything works 100% on my local build. I have triple checked all of my .htaccess files, but maybe I’m missing something.
The strangest part of all of this is that it was working on staging two days ago! I have no idea what is causing this.
Does anyone have any suggestions on how I might go about troubleshooting this? Or, by chance, a solution?
1: Clear your cache AND sessions .
2: Check if your module is installed using the free/open-source Module List Module
3 Drop some debugging code in the following method. The var_dumps will tell you which files/classes Magento's routers are looking for with your module, but can't find.
File: app/code/core/Mage/Core/Controller/Varien/Router/Standard.php
protected function _validateControllerClassName($realModule, $controller)
{
$controllerFileName = $this->getControllerFileName($realModule, $controller);
if (!$this->validateControllerFileName($controllerFileName)) {
var_dump($controllerFileName);
return false;
}
$controllerClassName = $this->getControllerClassName($realModule, $controller);
if (!$controllerClassName) {
var_dump($controllerFileName);
return false;
}
// include controller file if needed
if (!$this->_includeControllerClass($controllerFileName, $controllerClassName)) {
var_dump($controllerFileName);
return false;
}
return $controllerClassName;
}

Resources