Yii2 web url without using setAlias - php

I have a Yii2 app but no ability to edit the config files to utilize setAlias.
If my domain is example.com, and my Yii application is in /my-app directory, then what would be the Yii command to return the following string:
http://example.com/my-app/
Yii::getAlias('#app') returns
/httpdocs/my-app
Many posts exist but all I've found is suggestions either based on Yii1 or using setAlias in application config.

Yuo can use a simple urlHelper (for absolute url)
use yii\helpers\Url;
echo Url::to(['/'], true );

Related

Yii2 Console: app\models Class not found but common\models found

I have an Yii2 web application with 50+ model class files that are located in /models directory.
Now I want to run some console scripts from /console/controllers/MyController.php using these models but get class app\models\ModelName not found error, despite of use app\models\ModelName at the top.
If I copy a model file to /console/models/ModelName.php or /common/models/ModelName.php (and make change in use) it works alright. Is there any option to use models from /model or should I refactor the application so that both web and console use model files from /common/models
If you are using yii2-advanced build take in mind that #app alias is set each time depending on what part of application you are using.
If you are making a call from frontend, #app will be equal to /path/to/project-root/frontend.
If from backend - /path/to/project-root/backend.
console - /path/to/project-root/console
You may add custom alias in /common/config/bootstrap.php, to make your classes available from root.
For example try to add Yii::setAlias('#root', dirname(dirname(__DIR__))); to /common/config/bootstrap.php and set namespace to root/models
Note: if you will try add #app to bootstrap.php, it will be automatically reassigned by framework.
Note 2: You may check how yii2 autoloader works in BaseYii.php

Codeigniter how to access views/web pages from third party folders

I am in the process of installing SimpleSAML and in the php library, there is a folder called www that has index.php. According to the docs, there is an admin console within it. However, at the moment I am unable to access it via the url www.website.com/third_party/simplesaml/www/index.php.
I am supposed to use the admin console to generate some metadata so I'm just wondering if it is possible to route to a view from there?
I'm thinking that I create a controller and just hard link $this->load->view('url to www') but I'm not sure if that works.
In controller’s constructor add
include APPPATH . 'third_party/simplesaml/www/index.php';
to include the file in your project.
you can set base path in route file and instead of $this->load->view() you can use renderView() function to access view in codeigniter.

Angular 2 + angular-cli + Laravel 5.3

Using latest angular-cli, I created new project and everything works fine. Next, I tried to integrate it in Laravel 5.3. I have this project working with systemjs, but I want to switch to webpack and to take advantage of angular-cli.
Problem is that in angular-cli.json I can't specify that index is index.php, it only accepts HTML.
Basically, I can't start the Angular application at all with this setup.
How can I overcome this?
In the end I separated Laravel and Angular 2, as Cristian Sepulveda wrote in the comment. This is the recommended approach anyway.
I make API with Laravel and use it with Angular 2.
In my case I serve the angular app from laravel. I still use webpack to build my assets but have a gulp task which copies the angular index.html to be index.blade.php of which the laravel app serves.
I also use gulp to copy the built files from /dist to /public
I had the same problem and what I found is this related issue in their GitHub issues:
The output folder will always be entirely replaced. You can use the public/ folder to have your index.php which will be copied to your output folder, or output the app to a separate folder and copy the files yourself.
This is by design and will not change. This is a build output folder, not a deploy folder. You should separate those two steps.
So, you can't really achieve what you exactly want, but this is the only workaround I found.
I found only one solution for me.
create build for client side code by ng build --prod
Using gulp copy generated files into Laravel public dir gulp copy (here you can check if old build files exists remove them)
Using gulp-ingect plugin inject copied files into layout gulp inject
-- This can be used in CI and done with automation tools. In result we have inline.js and three *.**.bundle.js files injected. In same main layout i have statically add <base href="/example"> (you can use any defined in Laravel routes root path here) and inside template file which loaded from this path (in my case 'example.blade.php') add angular 2 root element <st-example>Loading...</st-example>
-- By this set up you have root Laravel layout which have inside required by angular 2 root url href and injected scripts files from build. And your template file for current route have root element inside (it included to main layout by simple blade yeild('content')).
P.S. also you must notice that if you are using some http requests in angular 2, after you integrate it into Laravel project this will add csrf protection middleware to each request... And if you have some new errors in requests which work previously just check headers.
Since angular-cli doesn’t allow you to specify index.php, let it be, simply specify index.html then there…
And add an appropriate route into Laravel routing. Like this one, for instance:
Route::any('{path?}', function () {
return File::get(public_path() . '/index.html');
})->where("path", ".+");
Btw, it’s simply a trap for any unknown routes… But I think you get an idea.

Testing multi-tenancy Laravel applications using Behat / Mink and Behat Laravel Extension

I am building a multi-tenant SaaS application which I am trying to write tests for with Behat, using Mink and the Behat Laravel Extension
When you register for an account, you get your own subdomain on the site {account}.tenancy.dev
my behat.yml file looks like so:
default:
extensions:
Laracasts\Behat:
# env_path: .env.behat
Behat\MinkExtension:
default_session: laravel
base_url: http://tenancy.dev
laravel: ~
I am having problems straight off the bat as when I try to test my registration flow, I am getting a 404 error testing that the new subdomain is accessible, all of the data has been saved correctly, manually testing the process works and the subdomain routing works.
I was wondering if there was any way to do this using Behat and how I would go about setting Behat / Mink to use wildcard subdomains to test SaaS applications?
I am running the test inside the Homestead VM.
The base_url: http://tenancy.dev configuration is used to generate a fully qualified domain URL when you utilize relative path URL's in your mink steps (IE "/home").
When you want to hit a domain different from the domain specified in base_url, all you have to do is use the fully qualified domain URL in your step like "http://test.tenancy.dev/fully/qualified".
So use the base_url configuration to set what you will be using for the majority of your steps as relative url's and then explicitly specify the full domain for the exceptions.
When I create an account named foo
And GET "http://foo.tenancy.dev/ping"
Then I get a 200 response code
When I GET "/home"
Then the response contains "Sign Up"
If the majority of your testing will be against the sub domain, set that as your base_url and explicitly specify your top level domain when necessary.
You may resolve subdomains using xip.io, which is especially useful if you cannot access the /etc/hosts file on a CI server, for example.
To route {account}.tenancy.dev to your local webserver, you can use account.tenancy.dev.127.0.0.1.xip.io which resolves to 127.0.0.1.
After a short while I revisited this problem and found a rather simple solution to be used in my FeatureContext.php:
$this->setMinkParameter('base_url', $url);
This changes the base url for any scenario it is used in:
/**
* #Given I visit the url :url
*/
public function visitDomain($url)
{
$this->setMinkParameter('base_url', $url);
$this->visit('/');
}
Which is used in the following way:
Scenario: Test Multi Tenancy
Given I have a business "mttest"
When I visit the url "http://mttest.example.com"
Then I should see "mttest"
Obviously this is slightly contrived but does show that what I was intending to do is possible.

Laravel - Artisan gives wrong base url

My app/config/app.php has
'url' => 'http://dev.domain.com/something/somethingElse'
Then I have a function that can be called from the application and also from an artisan command. But URL::route('myRoute') returns different results. When called from the application it returns http://dev.domain.com/something/somethingElse/myRoute, but in the artisan command http://dev.domain.com/myRoute.
URL::to has same behaviour.
Note: I do not have any other app.php file defined for other environments that could overwrite the global one.
Any ideas why this happens ?
Thanks!
A Laravel-generated URL consists of a number of parts:
Scheme: http://, https://, etc.
Host: dev.domain.com, localhost, etc.
Base: something/somethingElse (the subdirectory on the web server)
Tail: myRoute, (the Laravel route parameters)
These parts are then concatenated to form the URL.
Ultimately, Laravel generates the URL using the $_SERVER request variables. The function prepareBaseUrl() in Symfony\Component\HttpFoundation\Request is what is ultimately used to determine the base part of the URL.
When you make a request through your web browser, the request goes to ~/public/index.php and the necessary $_SERVER variables are populated with the correct information for Laravel to populate the base part of the URL.
However, when you make the request using Artisan on the command line, the request goes to the ~/artisan script. This means that the $_SERVER variables are not populated in the same way and Laravel is not able to determine the base part of the URL; instead, it returns an empty string ''.
From what I can find, it doesn't look like there is any appetite from the Laravel team to enable the application to function out-of-the-box in a subdirectory, e.g. Bugfix: domain routing for subfolder.
I ended up working around it in the way described by #medowlock for my scripts that would be called from the command line, e.g.:
Config::get('app.url') . URL::route('route.name', ['parameter'=>'value'], false)
This concatenates the application URL set in the app/config/app.php and the relative URL route.
If you use Laravel 4.2, you can call the URL::forceRootUrl function to explicitly define what the root url of your laravel application is.
URL::forceRootUrl( Config::get('app.url') );
Calls to URL::to will use the URL define in your app config file after forcing the Root URL.
Unfortunately, this isn't available in Laravel 4.1 or Laravel 4.0

Categories