How to get domain root url in Laravel 4? - url

I'm ready to scream how hard can this be? I've been trying for too long.
If I have http://www.example.com/more/pages/page.php or similar I want to be able to get
www.example.com.
Thats all. So I can use it as I please. This will of course change if on production or development so I want to ascertain it dynamically.
Request::root()
returns http://www.example.com/more/pages/page.php
URL::to('/')
returns http://www.example.com/more/pages/page.php
How do I get this? Why am I having so much trouble to do this??

UPDATE (2017-07-12)
A better solution is actually to use Request::getHost()
Previous answer:
I just checked and Request::root(); does return http://www.example.com in my case, no matter which route I'm on. You can then do the following to strip off the http:// part:
if (starts_with(Request::root(), 'http://'))
{
$domain = substr (Request::root(), 7); // $domain is now 'www.example.com'
}
You may want to double check or post more code (routes.php, controller code, ...) if the problem persists.
Another solution is to simply use $_SERVER['SERVER_NAME'].

You also may test any of these:
Request::server ("SERVER_NAME")
Request::server ("HTTP_HOST")
It seems better than making any treatment of
Request::root()
All right.

In Laravel 5.1 and later you can use
request()->getHost();
or
request()->getHttpHost();
(the second one will add port if it's not standard one)

My hint:
FIND IF EXISTS in .env:
APP_URL=http://yourhost.dev
REPLACE TO (OR ADD)
APP_DOMAIN=yourhost.dev
FIND in config/app.php:
'url' => env('APP_URL'),
REPLACE TO
'domain' => env('APP_DOMAIN'),
'url' => 'http://' . env('APP_DOMAIN'),
USE:
Config::get('app.domain'); // yourhost.dev
Config::get('app.url') // http://yourhost.dev
Do your magic!

This is for Laravel 5.1 and I am not sure does it work for earlier versions but if somebody search on Google and lands here it might be handy in middleware handle function gets $request parameter:
$request->server->get('SERVER_NAME')
outside of middleware handle method you can access it by helper function request()
request()->server->get('SERVER_NAME')

use directly where you want controller or web.php
Request::getHost();

I think you can use asset('/')

Related

How can I show the name of the link without http://, https://, and everything that goes after .com and other similar domains?

In my view I'm displaying the link in a such way:
<%= #casino.play_now_link %>
So, #casino.play_now_link can be like this: https://www.spinstation.com/?page=blockedcountry&content=1 What I need, is to display only this part: www.spinstation.com. I tried gsub('http://', '').gsub('https://', ''), and it works, but how can I remove the part of url name after .com? Thanks in advance.
Don't use regexes at all for this sort of thing, use URI from the standard library:
URI.parse(#casino.play_now_link).hostname
or, for a more robust solution, use Addressable:
Addressable::URI.parse(#casino.play_now_link).hostname
Of course, this assumes that you've properly validated that your play_now_links are valid URIs. If you haven't then you can add validations that use URI or Addressable to do so and either clean up existing play_now_links that aren't valid URIs or wrap the parsing and hostname extraction in a method (which is a good idea anyway) with some error handling.
In a simple way one can use
.split('/')[2]
which is regex based and depends on the '/' in your url.
But as #mu is too short mentioned: URI is better for this.

Yiiframework - parse a url string to (module, controller, action) array

I am looking for a tool(class or method or ...) in Yii that, get a string (a url) and return an array contains module, controller, action and other get_params.
I cannot use explode because it is possible that module dose not exists or can not find out that 'a/b' is module/controller or controller/action !
This is kind a weird of Yii - why I can't just do something similar to:
$this->uri->segment(1);
Many FWs have this feature, so simple and obvious.
PS Also, why I should write methods in Helpers like above to do things FW must definitely do? And yes I don't want to act like:
$request = new Request();
$request->setUrl('/site/contact');
Yii::$app->urlManager->parseRequest($request);
This is totally stupid, not after setting the url - just f***ing grab it like in old wonderful days. They should write an alternative either.

Wilcard in url patterns Yii Framework

I'm trying to create a dynamic url pattern for the following url:
http://domain.com/content/pagetitle
This is what I have added in the url.rules:
'content/<page:.*?>' => 'cms/default/home',
this works fine for /content/pagetitle.html but not for /content/pagetitle while my url suffix is empty. Does anyone know what I'm doing wrong?
Your problem might also be that the regex "any character" character . gets escape if outside of a named group.
<controller:[a-zA-Z]>/(.*)
turns into
<controller:[a-zA-Z]>/(\.*)
// ^ It escaped it for us even though we didn't want it
The solution is simply to make group for it:
<controller:[a-zA-Z]>/<wildcard:.*>
Not sure why you are adding the .*? there...
The following example should work
'content/<page:.+>' => 'cms/default/home',
You do not need a wildcard in this case:
'content/<page>' => 'cms/default/home',

Handling question mark in url in codeigniter

I am using a payment method which on success returns a url like mysite/payment/sucess?auth=SDX53641sSDFSDF, but since i am using codeigniter, question marks in url are not working for me.
I tried routing but it didnt work. As a final resort i created a pre system hook and unset the GET part from the url for with i had to set
$config["uri_protocol"] = "REQUEST_URI";
It worked that way but all other links in my site were not working as intended, tried changing the uri_protocol but could not make it work by any means.
So basically my problem is handling the ?auth=SDFSEFSDFXsdf5345sdf part in my url, whenever the paypment method redirects to my site with the url mentioned above, it gets redirected to homepage instead of the function inside controller.
How can i handle this, i'm using codeIgniter 1.7 version, and couldnt find any way.
Please suggest some solution.
I think I would extend the core URI class, by creating new file at application/libraries/MY_URI.php which will extend the CI_URI class, then copy the _fetch_uri_string method and here you can add your logic if the $_SERVER['QUERY_STRING'] is present:
class MY_URI extends CI_URI
{
function __construct()
{
parent::CI_URI();
}
//Whatever this method returns the router class is using to map controller and action accordingly.
function _fetch_uri_string()
{
if(isset($_SERVER['QUERY_STRING']) AND !empty($_GET['auth']))
{
//Do your logic here, For example you can do something like if you are using REQUEST_URI
$this->uri_string = 'payment/success/'.$_GET['auth'];
return;
//You will set the uri_string to controller/action/param the CI way and process further
}
//Here goes the rest of the method that you copied
}
}
Also please NOTE that you must do security check of your URL and I hope this works for you, otherwise you can try extending the CI_Router class or other methods (experiment little bit with few methods, though the _set_routing is important). These 2 classes are responsible for the intercepted urls and mapping them to controller/action/params in CI.
I believe this thread holds the answer.
Basically add
$config['enable_query_strings'] = TRUE;
to your config.php
hope this will help you ! no need to change htaccess,just change your code.
redirect(base_url()."controller_name/function_name");
Looking at the example code in the Stripe docs:
https://stripe.com/docs/payments/checkout/set-up-a-subscription
one might conclude that you can only retrieve a checkout_session_id as a named GET parameter like this:
'success_url' => 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
After playing around with .htaccess and config settings (as in previous answers) and finally getting it to work, it suddenly dawned on me that the DEVELOPER is in control of the success_url format, not Stripe. So one can avoid all the problems with a GET parameter by specifying the success url in the normal, clean Codeigniter format, like this:
'success_url' => 'https://example.com/success/{CHECKOUT_SESSION_ID}',
In my routes file, the incoming CHECKOUT_SESSION_ID string is passed to the Subscription controller's checkout_success method with this route:
$route['success/(:any)'] = 'subscription/checkout_success/$1';
The method accepts the $checkout_session_id like this:
function checkout_success($checkout_session_id)
{
// Do something with the $checkout_session_id...
}
I'm new to Stripe Checkout but this seems to simplify the integration without breaking Codeigniter's GET processing rules.

Rails route rewriting

I think this is an easy question. I am using this useful Flash Document Reader called FlexPaper. I have it embedded in one of my Show pages. But when I click the a link on their tool bar to show the document in a new browser, it points to the following link:
http://example.com/intels/FlexPaperViewer.swf?ZoomTime=0.5&FitPageOnLoad=false&PrintEnabled=false&SwfFile=%2FPaper.swf
which doesn't work, I get the following error:
ActiveRecord::RecordNotFound in IntelsController#show
Couldn't find Intel with ID=FlexPaperViewer
but if I remove the "intels" from the path so the url looks like:
http://example.com/FlexPaperViewer.swf?ZoomTime=0.5&FitPageOnLoad=false&PrintEnabled=false&SwfFile=%2FPaper.swf
It works fine.
My question is what is the best way to handle this? Can you write a route that rewrites a url that starts with intels/FlexPaperViewer.swf and remove the intels prefix? What would that look like?
Is there a better option?
Juat a thought, how about placing the FlexPaperViewer.swf inside public/intels folder?
So the directory structure will be
<project-directory>/public/intels/FlexPaperViewer.swf
Doing this will make the link correct. It seems to be easier to do it this way.
Hopefully it helps.
EDIT
Another alternative will be to see how the link was generated, maybe there is a parameter that you can set when you embed the FlexPaper.
I am not aware of any route modification that can do what you want. Maybe someone else can help on this.

Resources