Laravel. Add port to home url - laravel

Site use port 8080 on new server.
I need "site.com" => "site.com:8080"
How change base url in laravel for correct including css/js and redirects?

Ok, we tried many things.
It seems it's a bug or something. They have it since Laravel 4 and do not want to fix it:
https://github.com/laravel/framework/issues/1833
The only solutions I see here are:
1. To use standard port.
2. To generate URLs manually. For example:
// in controller
$baseUrl = 'http://'.$_SERVER['HTTP_HOST'].'/';
// in template
<link hre="{{ $baseUrl.'css/myStyleSheet.css' }}" ....>
Or you could set this variable in your top layout, so all inherited views have it.

I ended up adding the following line to boot() function of AppServiceProvider.php :
URL::forceRootUrl(env('APP_URL'));
You can also hardcode it there, but this way it uses the settings from .env file.
Warning: if the APP_URL is https, then I also needed to add the following line, or otherwise it would alter it to http (weird):
URL::forceScheme('https');

Related

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

Redirection issue in Codeigniter4

I have done admin controller and put that in a sub folder named 'Admin'
Controller
Admin
-login.php
Now I want to fetch that by router file where I wrote this
$routes->get('admin', 'Admin/Login::index');
But it is showing me "Not found" error and redirects to "http://localhost/admin".
Could there be some .htaccess issue?
replace this
$routes->get('admin', 'Admin/Login::index');
with
$routes->get('admin', 'Admin\Login::index');
also make sure you add namespace in your login.php
namespace App\Controllers\Admin;
If you keep CI4's directory structure intact you could in fact use sub-folders for Controllers, Models, Views, etc.
For example app/Controllers/Admin/Login.php is a valid place to put a Controller class. Make sure to add the appropriate namespace in Login.php - namespace App\Controllers\Admin; Also in routes - $routes->get('admin', 'App\Controllers\Admin\Login::index'); It is quite possible to work without the prefix of App\Controllers, but I never extensively tested it and I think there was a problem in some versions of CI4 before.
Another issue could be your app/Config/App.php class. If you did not change anything in your .htaccess file (the one in public directory!), $baseURL should be set to your public directory address - http://localhost/myproject/public/ . Or if you wish to make it easier - set up virtual hosts.
Just a thing to add - get() method in $routes allow only GET requests, meaning if you are trying to POST something (or use any other HTTP request method) it will fail and redirect.

Laravel 5 maintenance mode turn on without artisan

Is there any possibility to turn on and turn off Laravel 5 maintenance without php artisan up and down commands when my website is being hosted ?
What I've done:
Route::get('site/shutdown', function(){
return Artisan::call('down');
});
Route::get('site/live', function(){
return Artisan::call('up');
});
The first route is working fine. But when I call site/live the site still is shuted down. What can cause this problem ?
If your project is already down, you cannot call another function.
What happens after you run php artisan down is that it creates a file named down inside storage/framework. After running php artisan up the file is removed.
You can create the file manually inside storage/framework. It will down your project. When you want to take your project live again, just remove the file.
I think the right answer is missing here..
You could add your route to app/http/middleware/CheckForMaintenanceMode.php
protected $except = [
//here
];
So It never would be off.
when you run artisan down. site is not available so when try to call up, your IP can't access site.
you must call down with your IP exception.
php artisan down --allow=127.0.0.1 --allow=192.168.0.0/16
or add ::1 to local.
to make that in route without command
try to save this command in specific one and call it.
Laravel 8 introduced secret in maintenance mode, in which you can bypass the maintenance mode by providing a secret, then your Artisan::call would work.
You could add your routes to the $except var in CheckForMaintenanceMode middleware to bypass the check. Then your site/live route would work just fine.
In order to make your site live again using an url, you can create a live.php file which you put in laravel's public folder and then visit http://your.domain/live.php .
In the live.php file you need something like this: (check your projects directory structure if you don't use the default public folder!)
<?php
unlink(dirname(__FILE__) . "/../storage/framework/down");
header("Location: your.domain");
die;
just put
Artisan::call('up');
without route function.

Laravel 4 - changing resource root routing path

In a Laravel 4 installation, Using Jeffrey Way's Laravel 4 Generators, I set up a 'tweet' resource, using the scaffolding command from his example:
php artisan generate:scaffold tweet --fields="author:string, body:text"
This generated the model, view, controller, migration and routing information for the tweet type. After migrating the database, visiting http://localhost:8000/tweets works fine, and shows the expected content.
The contents of the routes.php file at this point is:
Route::resource('tweets', 'TweetsController');
Now I would like to move the url for tweets up one level into admin/tweets, so the above url should become: http://localhost:8000/admin/tweets. Please note that I am not treating 'Admin' as a resource, but instead just want to add it for hypothetical organizational purposes.
Changing the routes.php file to:
Route::resource('admin/tweets', 'TweetsController');
Does not work, and displays the following error:
Unable to generate a URL for the named route "tweets.create" as such route does not exist.
Similarly when using the following:
Route::group(array('prefix' => 'admin'), function() {
Route::resource('tweets', 'TweetsController');
});
As was suggested in this stackoverflow question.
Using php artisan routes reveals that the named routes also now have admin prefixed to them, turning tweets.create into admin.tweets.create.
Why is the error saying that it cannot find tweets.create? shouldn't that automatically be resolved (judging by the routes table), to use admin.tweets.create?
How can I change my routing so that this error no longer occurs?
I just tested with new resource controller and it works fine for me.
The problem is not with the Route, its with the named routes used in your application.
check your view files there are link to route like link_to_route('tweets.create', 'Add new tweet'), this is creating the error because when you add admin as prefix tweets.create doesn't exists so change it to admin.tweets.create every where, in your controller also where ever named route is used.

Routing issue with 404_override

I'm having an issue with 404_override in CI 2.02. Here is my default controller and override:
$route['default_controller'] = "home/index_controller";
$route['404_override'] = "misc/site_map";
This line uncommented gives me this error:
Unable to load your default controller. Please make sure the
controller specified in your Routes.php file is valid.
But commented, I get a 404 error. So something in the override is causing the problem. I just don't know what. I've tried various MY_Router files and they don't help. Anyone have any suggestions about how I can fix this?
I've eliminated my .htaccess file as the problem by deleting it from the server and then trying to access a controller that doesn't exist like this: http://domain.com/index.php/doesnotexist. I still get the error.
For anyone else coming here with this issue, this is/was a bug in CodeIgniter itself to do with 404 pages not being able to be included in subfolders.
Bug Discussion Including Fix on GitHub
You have forgotten to add the default controller that will be used when you first load your website, in your routes.php you need to add this, it is required which is why you're seeing the errors being thrown.
$route['default_controller'] = 'index'; // this will need to be changed to your default controller
$route['404_override'] = 'misc/site_map';
EDIT:
Okay, you must make sure you enter the correct controller name you wish to load as the default, for example you have home.php in your controllers folder, you must enter home as the default. Optionally, you can define segments that are functions in your home.php class file, e.g. home/function_name.

Resources