I am currently working on a "multisite" feature for my current laravel 4 project (just like WordPress has it).
The simplest approach (in my opinion) would be to copy the public folder for each site, then it still uses the same core but you can also edit the assets for each site. The only thing that still needs to get accomplished is the different database configurations for each site.
I tried to detect the environment based on the name/path of the currently used public folder. However when i try to use public_path() in the bootstrap/start.php file it will lead to this error:
Fatal error: Call to a member function make() on a non-object in
vendor\laravel\framework\src\Illuminate\Support\helpers.php on line
685
How can I do that?
in laravel, it is actually more simple.
Route::group(['domain' => 'subdomain1.example.com'], function(){
//rotues for subdomain 1
});
Route::group(['domain' => 'subdomain2.example.com'], function(){
//rotues for subdomain 2
});
i use nested controllers with namespaces for each domain.
e.g.
Subdomain1
controllers/subdomain1/HomeController
....
Subdomain2
controllers/subdomain2/HomeController
....
for Config
Config/subdomain1/database.php (e.g.)
and
Config/subdomain2/database.php (e.g.)
and then setup the environment
$env = $app->detectEnvironment(array(
'local' => ['*.dev', gethostname()],
'subdomain1' => ['subdomain1.example.com'],
'subdomain2' => ['subdomin2.example.com']
));
....and that's it.
Related
I made this in:
/vendor/backpack/permissionmanager/src/routes/backpack/permissionmanager.php
Route::group([
'namespace' => 'Backpack\PermissionManager\app\Http\Controllers',
'prefix' => config('backpack.base.route_prefix', 'admin'),
'middleware' => ['web', backpack_middleware()],
], function () {
Route::group(['middleware' => ['can:edit permissions']], function () { <---- I added this code
Route::crud('permission', 'PermissionCrudController');
Route::crud('role', 'RoleCrudController');
Route::crud('user', 'UserCrudController');
}); <---- I added this code
});
It doesn't work
But I know is not a good idea to do it like this, touching the vendor folder
How can I handle this situation?
Also I've tried to put that code inside the routes of Backpack (/routes/backpack/custom.php), with no results
Indeed, changing things in the vendor directory is a big no-no. But you can easily do the same in your app. You can see in this package's docs, under overwriting functionality that there's an easier solution for that:
Create a routes/backpack/permissionmanager.php file. Then Backpack will load YOUR file, instead of the one in the package.
Remember to include ALL routes from the vendor file and all your overwrites too. The file in the vendor will no longer be used.
I have got a Laravel website a: www.website.com and b: www.website.co.kr.
As you can see website b has a different extension 'co.kr'.
Both urls route to the same server. Most pages are the same for both websites, but some pages are different.
I want to handle this in my routing.
So for example:
www.website.com/about-us
www.website.co.kr/about-us
Shows a different page.
Right now I handle this using a group:
$co_kr_routes = function () {
Route::get('/about-us', [
'uses' => 'Frontend\Korea\PagesController#getAboutUs',
]);
};
Route::group(['domain' => 'www.website.co.kr'], $co_kr_routes);
//Default
Route::get('/about-us', [
'uses' => 'Frontend\PagesController#getAboutUs',
]);
This works, but I am not satisfied with the solution and it is causing me not being able to cache the routes 'php artisan route:cache' because of the duplicate route '/about-us'.
My question is:
Is there a nicer way to handle different extensions but same server resulting in showing a different page?
I didn't find much help googling the issue.
Right now I have to very specifically give the .co.kr domain to the group but it would be nicer if I could filter on 'co.kr' instead of the full domain.
I thought a solution would be handling this in the controller method itself, but handling this using routes sounds like the way to go. I dont think the controller should know or care if a request was made from a .com or .co.kr extension.
I want to change the Laravel 5.1 storage path: something like /home/test/storage. This has the advantage that these files are not stored in the repository, which is fairly ugly I think.
In Laravel 4, this was very simple with bootstrap/paths.php.
In Laravel 5, it works by using $app->useStoragePath('/path/') in bootstrap/app.php. However, I want to define the storage path with a config option, like $app->useStoragePath(config('app.storage_path'). The config option calls an environment variable or returns a default location.
Doing this results in a Uncaught exception 'ReflectionException' with message 'Class config does not exist'; this makes sense, because this function is not loaded yet.
I tried setting the storage path just after booting:
$app->booted(function () use ($app) {
$app->useStoragePath(config('app.storage_root'));
});
This changed nothing. I also tried directly binding it to path.storage:
$app->bind('path.storage', function ($app) {
return config('app.storage_root');
});
The last option works partially; the view cache is now placed in the correct location, but the logs are still at the old location.
Set it up in .env
app.php
'app_storage' => env('APP_STORAGE', storage_path()),
app/Providers/AppServiceProvider.php
public function register()
{
$this->app->useStoragePath(config('app.app_storage'));
}
.env
APP_STORAGE=custom_location
Laravel 5.3 is in bootstrap/app.php
/*
|--------------------------------------------------------------------------
| Set Storage Path
|--------------------------------------------------------------------------
|
| This script allows us to override the default storage location used by
| the application. You may set the APP_STORAGE environment variable
| in your .env file, if not set the default location will be used
|
*/
$app->useStoragePath( env( 'APP_STORAGE', base_path() . '/storage' ) );
Here is a simple solution of changing the storage path in Laravel 5 like we do in Laravel 4
on bootstrap/app.php
# new storage path
# base_path() -> returns root path
$path_storage = base_path() . "../../storage";
# override already $app->storagePath using the function
$app->useStoragePath($path_storage);
this will make the storage path to be same with the session, views, cache, logs
This works on Laravel 5.2
File: app/Providers/AppServiceProvider.php
public function register() {
...
$this->app->useStoragePath(config('what_ever_you_want'));
...
}
Calling useStoragePath on your AppServiceProvider wouldn't do the job properly because the AppServiceProvider is called after the config files are loaded. so any use of storage_path in config files would still refer to the old storage path.
In order to properly solve this problem I suggest you extend Application class and then on the constructor of your own class write the followings.
/**
* MyApplication constructor.
*/
public function __construct($basePath = null)
{
parent::__construct($basePath);
// set the storage path
$this->afterLoadingEnvironment(function () {
$this->useStoragePath(/*path to your storage directory*/);
});
}
if your website is hosted;
move everything from public folder to root folder
$app->bind('path.public', function() {
return __DIR__;
});
add this code in your bootstrap/app.php :
$app->useStoragePath(__DIR__ . '/../custom-path/');
and in config/filesystem.php :
'local' => [
'driver' => 'local',
'root' => storage_path('everything'), //custom-path/everything
],
'public' => [
'driver' => 'local',
'root' => storage_path(''),
'url' => env('APP_URL'),
'visibility' => 'public',
],
and then run php artisan config:cache
This works on Laravel 5.8 because you have to load the environment variables before you try to use them.
File: bootstrap/app.php
$app->afterLoadingEnvironment(function() use ($app) {
$app->useStoragePath(env('APP_STORAGE', storage_path()));
});
This is a question about Yii 2. I have already done this for Yii 1.x (previous versions), but Yii 2 seems quite different.
For a static web site, I need to set my base URL to http://www.sampledomain.com
I understand that default server name can be used. However I'd like to hard code my domain name to some one place in the framework.
In my /config/web.php (config file). I have this configuration.
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
...
Currently Yii::$app->request->BaseUrl returns and empty.
If this is all about setting and getting Yii baseurl. Could anyone please help by showing me how the code above can change, so I can set the base URL to http://www.sampledomain.com
At the moment, a Yii 2 static site takes the $_SERVER['SERVER_NAME'] of the domain, so it uses the domain being visited. However if my static site has several parked domains, I want all my internal links within the website to use one domain only. Is this possible?
In params.php
add
return [
.....,
'domainName' => 'yourDomani.com',
]
and in controller you can call it by
Yii::$app->params['domainName'];
Currently my site is designed to serve up multiple subdomains like this:
Route::group(array('domain' => 'site1'.AppHelper::getDomain()), function(){
Route::get('{state?}/{variant?}', array("uses" => 'Site1Controller#showIndex'));
});
Route::group(array('domain' => 'site2'.AppHelper::getDomain()), function(){
Route::get('{state?}/{variant?}', array("uses" => 'Site2Controller#showIndex'));
});
Route::group(array('domain' => 'site3'.AppHelper::getDomain()), function(){
Route::get('{state?}/{variant?}', array("uses" => 'Site3Controller#showIndex'));
});
Now I want to write some tests that crawl these pages checking for specific content. Unfortunately, the only documentation I can find on the Symfony DomCrawler for subdomains tells me to do this (let's say this is on line 312 of my test):
$crawler = $this->$client->request('GET', '/', array(), array(), array(
'HTTP_HOST' => 'site1.domain.dev',
'HTTP_USER_AGENT' => 'Symfony/2.0',
));
Unfortunately when I run PHPUnit and it gets to this test I see this:
1) SiteTest::testSite1ForContent
Symfony\Component\HttpKernel\Exception\NotFoundHttpException:
/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1429
/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1050
/var/www/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1014
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:576
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:597
/var/www/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Client.php:81
/var/www/vendor/symfony/browser-kit/Symfony/Component/BrowserKit/Client.php:339
/var/www/app/tests/SiteTest.php:312
So what is it that I'm not understanding here? The site1.domain.dev definitely resolves normally and has no issues that I can see. It seems to be a problem specific to trying to force the header for the subdomain.
Even apart from a solution, how do I debug this properly? I want to figure out why it's not working/where it's failing since this is my first attempt at writing tests.
I started a blank project that just tried to handle this with subdomain routes and it worked perfectly. I realized the issue had to be with what I wrote.
Turns out the issue was actually with a helper I wrote to retrieve the domain. It was trying to access $_SERVER vars that weren't available to the tests when running. Having that return a dev domain if it couldn't retrieve the values solved my issue.
Try this one:
public function refreshApplication()
{
parent::refreshApplication();
$this->client = $this->createClient(
array('HTTP_HOST' => 'site1.domain.dev'));
}
public function testSomething()
{
$crawler = $this->client->request('GET', '/something');
}