Sub-domain routing in Laravel on shared hosting - laravel

I'm using Laravel framework version 5.1 for my web application that will be run on a shared hosting. Since I have a very limited access to this host, (e.g. I don't have a SSH access, can't create virtual hosts inside the machine etc.) I had to make some tricks to run it smoothly. Here is what I've done so far:
Moved the files inside of the public/ directory to my root directory.
Changed the file paths of auto load registrar in the public/index.php with the new directory.
Everything works as intended except the sub-domain routing. I defined api.myapp.com as wildcard on routes.php but when I try to visit this subdomain, Chrome gives me DNS_PROBE_FINISHED_NXDOMAIN error. Here is the code from routes.php:
Route::group([
'domain' => 'api.myapp.com',
'prefix' => 'v1',
'middleware' => 'cors'
], function () {
// some RESTful resource controllers
});
How can I manage to work this subdomain? What am I missing?
Thanks in advance!

Related

How to set up a Laravel App with Subdomains to work on a cPanel server?

First of all: this is not a duplicate of (How to set up a laravel project on cPanel subdomain?) this question isn't the same as mine.
I would like to know how can i set up a whole Laravel application, with subdomains (not only one subdomain) to work on a cPanel server
So here is the test code i'm using on my routes/web.php file.
Route::domain('test.mydomain.com')->group(function() {
Route::get('/', function() {
return 'DOMAIN TEST.MYDOMAIN.COM';
});
});
Route::domain('mydomain.com')->group(function() {
Route::get('/', function() {
return 'DOMAIN MYDOMAIN.COM';
});
});
Route::get('/', function() {
return 'ROOT DOMAIN';
});
I'm currently using cPanel to handle domains and subdomains, but i have no idea how to make it work with Laravel Subdomains, i tried the code above.
When i access my main domain mydomain.com it shows "DOMAIN MYDOMAIN.COM" so it works ok, but when i go to test.mydomain.com it just access my subdomain folders from this subdomain, so, how can i make it works?
Maybe i need to put some .htaccess file to get this working properly? Can someone help?
Did you try to link subdomains to the same folder as primary domain ?
Eg.
domain.com => /public_html/public
test.domain.com => /public_html/public

Laravel keeps logging me out after login

I have Laravel set up. I am running multiple domains to it. Before yesterday I had one top level domain and a few sub domains. I added another TLD yesterday and everything started working weird. Specifically, when trying to log into the admin section it redirects to the home page. I have one admin but server domains coming to the site. My route group it:
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {
Sometimes is actually brings me into the admin but as soon as I click on a link I get logged out and redirected to the home page. I keep seeing 401 errors in the console as well.
What would cause this?
edit
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {
Route::get('/', 'AdminController#index');
Route::get('/pages', 'AdminController#pages');
Route::get('page/create', 'AdminController#createPagePage');
Route::post('createPage', 'AdminController#createPage');
Route::get('/page/edit/{page_id}', 'AdminController#editPagePage');
Route::post('editPage', 'AdminController#editPage');
});
$redirectTo = '/';
If you are using Linux, try to check the session file permissions and change them so the app can write session keys on that file and if it doesn't work, try to change the session driver to database.
how to change the session driver?
Go to your .env file and change SESSION_DRIVER=file to SESSION_DRIVER=database.
create a session migration: php artisan session:table.
composer dump-autoload.
Finally migrate (php artisan migrate).
Have you setting the SESSION_DOMAIN right?
One thing you may notice is that session isn’t saved when the user is logged-in in one store and goes to another. To fix that issue, we’ll need to add a SESSION_DOMAIN variable to our .env file, for example:
SESSION_DOMAIN=’.example.com’
That’s all, now user session will be shared across all shops/subdomains in our app.
https://medium.com/#maxkostinevich/how-to-build-multi-domain-laravel-application-e6d87d5e507b
If you are confident with the domain setup correctly. Please verify these few things.
1. Your config/session.php is correctly setup to support multiple domains.
'driver' => env('SESSION_DRIVER', 'file'), // check /storage folder writtable recursivelly.
'domain' => env('SESSION_DOMAIN', null), // verify if single domain is not given in .env or in settings.
'same_site' => null // it should not be strict if you have ajax request across these domain. I mean from javascript you may try to request to other domain.
etc...
2. Confirm, if your routes are not group under domain specific. something like:
Route::group(['domain' => 'xxx'], function($params)
{
// your routes..
});
3. If there is any hardcoded domain verification/conditions or redirection added in code.
4. Any values in .env poiting to single domain.
5. Also don't forgot to clear the cache/compiled view/routes and delete the temp files once. like generated files inside /app/bootstrap & /storage
// Clear Application Cache
php artisan cache:clear
// Clear route cache
php artisan route:cache
// Clear config cache
php artisan config:cache
// Clear compiled view files
php artisan view:clear
6. Any thirdparty package might contains the domain settings. Make sure to verify the files in your /config folder.
7. Check the url rewrite settings in .htaccess
8. Do confirm if there is any proxy settings are there or any software installed on server - very rare case.
In case still having issue, please restart the apache/nginx service once because sometimes the config changes not effected after change that might need server restart.
Hopefully it helps.

Laravel 5.5 Route groups

I was having this in my website using Laravel 5.3 :
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware'=>'auth'], function(){
Route::resource('posts', 'PostsController');
});
This lets me go to the admin panel using: mywebsite/public/admin/posts.
Now, when I migrated the site to Laravel5.5 I got this error Route[admin.posts.create] not defined when i attempt to open the link Create post which was working fine before.
I know that routing system has changed but I did not know how to have such links in new Laravel5.5. I tried url instead of route but I got the same error. I also checked the new documentation but I did not get exactly how to have the same link system.
Can anyone have a better explanation of this new routing system? (I have to migrate the site to 5.5).
Laravel names resource routes by default, you can check them by running php artisan route:list
If you want to override them for any reason you can pass in an array when you define the route and override each individual route name like so:
Route::resource('posts', 'PostsController', ['names' => [
'create' => 'admin.posts.build'
]]);

Laravel routes with domains and subdomains

I've an app developed with laravel 5 and I will deploy it in a digitalocean droplet.
Can I have multiple domains and subdmanins in my server that using this unique application?
How I should implement routes.php to involve that?
I use this code to subdomains on 'mysite.com'
Route::group(array('domain' => '{subdomain}.mysite.com'), function() {
Route::get('customer/{customerId}', function() {
//blablabla;
});
});
But What I have to do if I have others domains like 'othersite.com'?
PS: All domains are hosted in the same droplet.
For subdomain, no problem (like said in http://laravel.com/docs/5.1/routing#route-group-sub-domain-routing)
For domain, you can, but with the url set in the config you'll have problem with link...
So, i'll do things like that :
Get the domaine name ( something like $request->server->get('SERVER_NAME') or request()->server->get('SERVER_NAME') )
update the config with \Config::set('app.url', $mydomainename)
You can do that in route, but maybe it will be better in a middleware.
And voila
Ps: take care about duplicate content with google, maybe some canonical will be usefull

YiiBoilerplate Url Rewriting - php yii

When we configure YiiBoilerplate. we use its frontend by these url
http://localhost/YiiBoilerplate-master/frontend/www/index.php/site/
i want to change this url and access it like
http://localhost/YiiBoilerplate-master/site/index.
how it is possible by mod_rewrite?
Add a .htaccess file to your webserver root:
RewriteEngine on
RewriteRule ^/YiiBoilerplate-master/(.*) /YiiBoilerplate-master/frontend/www/index.php/$1
This gives you access to your app via a shorter URL, but the links within the app still stay in the longer form. To update the URLs within your app, update baseUrl in the urlManager component:
'components' =>
'urlManager' => array(
'baseUrl' => '/YiiBoilerplate-master',
'urlFormat' => 'path',
...
Note: I'd strongly recommend setting up a virtual host.

Resources