webroot entire path in Yii2 - php

I have some trouble with an Yii2 application.
I need to put into a variabile the complete webroot path of my application, in my case www.mysite.it/language/catalogue/. When I call the function to retrieve this information, I get www.mysite.it/application/web where application/web is the root of my project.
I have tried this function:
$home = Yii::$app->request->getAbsoluteUrl();
It is any way to do what I want?
Thanks in advance.

You should use yii\helpers\Url features
http://www.yiiframework.com/doc-2.0/yii-helpers-url.html
http://www.yiiframework.com/doc-2.0/guide-helper-url.html
and eg_ for home Url::home()
$absoluteHomeUrl = Url::home(true);
or for Url:to()
$url = Url::toRoute(['language/catalogue/']);
the use of the UrlHelpers prevent your code from different url result related
to your urlManager config (with or without pretty url)

Related

Convert GET links to nice links

I'm using Codeigniter in my new web application, and I have a form in page which sends data via post to a server and that server returns the users back to my website but with the parameters via get with ugly links like example.com/id=12&code=Xxxx
What I would like to do, if it's possible and I've been searching and I can't find is how to convert those ugly links into nice friendly links ( example.com/12/Xxxx )
Thanks
You can't have a GET form transform into a nice URL directly. They will automatically become ?key=val format.
Your best option is to have a redirect header to translate the GET to the nice URL's.
eg:
$firstPart = $_GET['myKey'];
$secondPart = $_GET['mySecondKey'];
header('Location: ' . $requestURL . '/' . $firstPart . '/' . $secondPart);
Get parameters are basically standard for any Web API's so you will probably find that you will end up using them a lot so rather than hack fixes for each and every API create a middle man.
Create a new folder at the same level as your index.php (usually your site root). call this folder something like middleman
Add the middleman folder to the list of items in your .htaccess file that should not be routed through the index.php
Now for every API you use you can create a new .php file in the middleman folder which transforms all requests to one that your site can understand.
You then point your external API's to http://yoursite.com/middleman/api_name.php
The reason for creating the middleman is that Code Igniter obliterates the get array so it gets removed before you can process the requests and format them into something meaningful if you try to do it inside Code Igniter so we must perform the transformation outside of Code Igniter's scope.
an example file might looks like this:
mysite.pingback.php
<?php
$from = $_GET['ip'];
$time = $_GET['time'];
$post = $_GET['id'];
header('location: ../mysite/pingback/'.$from.'/'.$post.'/'.$time);
?>
So very simple files. but it also means that when a change is made to the external API it does not affect how your Code Igniter app works as you simple have to change your middleman.

Codeigniter Loading external site in default controller

I'm not able to figure this out on my own so here I am asking for your help.
How do I load a website that I already made as a view in the code igniter default controller?
I put my website under a folder name site, and in the default controller I loaded the view site/index , but then in my site there are problems with the includes and redirects... I don't know why, I guess the way the site usually works with redirecting isn't compatible with code igniter style
edit: I guess I would have to turn off CI engine for this site, but I don't know why, because I would still need codeingiter to manage other parts of my application
"CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'Blog';
Where Blog is the name of the controller class you want used. If you now load your main index.php file without specifying any URI segments you'll see your Hello World message by default."
http://codeigniter.com/user_guide/general/controllers.html
fragment copied from that link , you should put the controllers classname in that config, not the view
I guess it's better to choose one of these options:
Modify the existing site to a CodeIgniter site.
Keep your site separate from the CodeIgniter site, and just link between the two sites.
The way you are trying to do it seems very useless and causing a lot of extra trouble.
You can simply use the redirect function in your controller. If you supply a full URL you can go to any other page. You will, of course, leave your CI app.
redirect('http://www.example.net/page_in_external_site/');
Try using the APPPATH constant when defining the paths for the includes.
I know it's an old question, but you can try using a view template with an iframe, and you can pass the URL to the src property of the iframe. That way you can display your site inside a view, but still can't get access to the vars passed to the view from your site.
In system/Core/Loader.php change the line 141 to look like this:
$this->_ci_view_paths = array(APPPATH . 'views/' => TRUE, FCPATH => TRUE);
and to get the view is simple:
$this->load->view('application/ PATH_TO_VIEW');

Including codeigniter from a normal PHP file?

A 3rd party service that I'm using returns the users to a url like this:
site.com/something.php?id=XXX&something=abc....
Therefore, I need to be able to accept $_GET parameters for only one part of the site.
Is there a way to put a file outside of codeigniter's application directory which will do something like this:
<?
$id = $_GET['id'];
$something = $_GET['something'];
//Do something so codeigniter thinks this is a request to site.com/process/$id/$something
require('index.php'); //codeigniter's index.php file
?>
I remember using putenv() to achieve this in the past, but don't remember the details.
Not sure about putenv, but if worse comes to worse you can get the contents of the get array by exploding $_SERVER['QUERY_STRING']
get doesn't need to be enabled and you still have access.
Try just using a redirect:
<?php
$id = $_GET['id'];
$something = $_GET['something'];
header('Location: http://www.site.com/' . $id . '/' . $something);
As long as the initial something.php request is made directly to that file and not index.php, then CodeIgniter won't run (actually, that would depend on your .htaccess [or equivalent] file, so you may need to tweak it).
That's the safest way I can think to do it, and you won't have to break up CodeIgniter's program flow or enable query strings in your application, which may be unsafe.
Maybe I'm misunderstanding your architecture but couldn't you just enable GET for codeigniter..?
See $config['allow_get_array'] in the docs:
http://codeigniter.com/user_guide/libraries/input.html
And see $this->input->get() on the same page..
try
$this->config->set_item("allow_get_array",TRUE);
in your controller's constructor before parent::__construct();.
The security filtering function is called automatically when a new
controller is invoked. It does the following:
If $config['allow_get_array'] is FALSE(default is TRUE), destroys the
global GET array.

Zend Framework: From a folder (not /)

i notice that when i run a zend framework app from a server, there are alot of side effects. main issue is where i use urls like
/auth/login
i need to use
$this->baseUrl('/auth/login');
thats simple to fix. but when i use
$request->getRequestUri()
for use in redirects. eg after login, i want to redirect the user back to the prev page, it goes to the wrong place. eg. my app root is "http://localhost/app1", $request->getRequestUri() will give /app1. when i try to redirect back, it will goto http://localhost/app1/app1. btw, i am using Zend Server + IIS7 and my app is configured to run from the url stated above. maybe i shld be going to "/" instead. how can i resolve this?
update
this is in my Zend_Form class
// (Zend_Form) Login.php init()
$req = Zend_Controller_Front::getInstance()->getRequest();
$returnUrl = $req->getParam('returnUrl', $req->getRequestUri());
$this->addElement('hidden', 'returnUrl', array(
'value' => $returnUrl
));
// AuthController after login
$returnUrl = urldecode($form->getElement('returnUrl')->getValue());
if (!empty($returnUrl)) {
$this->_helper->getHelper('Redirector')->setGotoUrl($returnUrl);
}
Based on you update:
Its the prependBase-Option in the Redirector what you are looking for:
prependBase: boolean flag indicating whether or not to prepend the base URL when a relative URL is provided
So your fix is:
$this->_helper->getHelper('Redirector')->setGotoUrl($returnUrl, array('prependBase' => false));
If you are using Zend_Application you can use this in your application.ini, and wont need to specify anything else.
resources.frontController.baseUrl = "/your/public/path/"
I have solved this problem with help of Apache configs. In file \usr\local\apache\conf\vhosts.conf find a block with your site and change ways and public folder.

codeigniter, global variable for beta project path, and access from everywhere

I use CodeIgniter, I'm happy with that, but I have a question.
I build my projects under /www/projectname/beta/... directory, so at my code, at many parts like including some images or css files or etc. I have to make ... src="/projectname/beta/... so when I complete the website, I need to edit so many pages to clear these /projectname/beta/ path and make it / for main root. or when I start new project with same base, first of all I need to edit these paths at all files.
now, how can I define a variable like
$projectbetapath =
"/projectname/beta/";
and have access from everywhere, like global. where can I add such line, and how can I access this var from everywhere?
Thanks!! appreciate!
Why don't you add a constant in your index.php file?
define('BETA_PATH', '/beta');
When the site leaves the beta stage you just do:
define('BETA_PATH', '');
There are two answers to your question:
Set your variables as array fields of $config in application/config/config.php and access them with $this->config->item('name');
Use the URL-helper (Or $this->config->item('base_url')) to get the current base path whenever you have to type in a path.
The second answer will give you full flexibility, you'll only have to modify the base URL in config.php if the project moves.

Categories