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',
Related
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.
I am trying to use Deep Link Kit to route both of these paths:
myapp://page/2 // <- doesn't work
myapp://page/2/7 //<- works
The route handler I've registered at the moment is:
router.registerHandlerClass(AppRouteHandler.self, forRoute: "page/:number/:commentID(.*)")
I added the (.*) for the regex of zero or more comment IDs. However this doesn't seem to make any difference as it only works when you have both the :number and :commentID defined. I've also tried myapp://page/2/ but that doesn't work either. Any help would be appreciated.
UPDATE
One solution is to register the two routes separately:
router.registerHandlerClass(AppRouteHandler.self, forRoute: "page/:number")
router.registerHandlerClass(AppRouteHandler.self, forRoute: "page/:number/:commentID")
but ideally, I'd be able to use regex.
I counter this problem too, after combine you solution I came up with this solution
router.registerHandlerClass(AppRouteHandler.self, forRoute: "page/:number/?:commentID(.*)")
That will ignore the second /, and your commentID will be set with empty string
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('/')
I am trying to work out the best way to replace multiple variables/placeholders within localized message string in my ruby on rails application. When replacing a single placeholder I have used the satisfactory:
In en.yml:
url_borked: "The URL: $url could not be loaded."
In view:
t(:url_borked)["$url"] = request.url
But this is not suitable for multiple placeholders. It looks ugly, and it doesn't actually work e.g:
In en.yml:
url_borked: "The URL: $url is badly formatted, perhaps you meant: $url_clean"
In view:
(t(:url_borked)["$url"] = request.url)["url_clean") = #suggested_url
I have tried using String::sub, but am not happy with that as it ugly. e.g.:
(t(:url_borked).sub("$url", request.url).sub("url_clean", #suggested_url)
It also doesn't work if you want to replace multiple instances of the one placeholder. e.g.:
bad_url: "$url cannot be loaded, please try $url another time"
I have also considered the printf function, but that does not work for localisation as the relative position of the placeholder can change depending on the translation.
Is there correct way to do this message placeholder substitution?
Thanks.
Why not:
t(:url_borked, :url=>request.url, :url_clean=>#suggested_url)
?
Ok, I had a brainwave and looked at the I18n::translate function a bit more closely and found the "interpolation" functionality within.
e.g.
I18n.t :foo, :bar => 'baz' # => 'foo baz'
Exactly what I needed. Funny that I would work it out after I finally decided to ask the crowd for a solution :-)
Cheers.
I have the following Regexp to create a hash of values by separating a string at a semicolon:
Hash["photo:chase jarvis".scan(/(.*)\:(.*)/)]
// {'photo' => 'chase jarvis'}
But I also want to be able to have URL's in the string and recognize it so it maintains the URL part in the value side of the hash i.e:
Hash["photo:http://www.chasejarvis.com".scan(/(.*)\:(.*)/)]
// Results in {'photo:http' => '//www.chasejarvis.com'}
I want, of course:
Hash["photo:chase jarvis".scan(/ ... /)]
// {'photo' => 'http://www.chasejarvis.com'}
If you only want to match up to first colon you could change (.*)\:(.*) to ([^:]*)\:(.*).
Alternatively, you could make it a non-greedy match, but I prefer saying "not colon".
How do figure out a person's family name and first name?
Changing chasejarvis to chase and jarvis might not be possible unless you have a solution for that.
Do you already know everyone's name in your project? Nobody is having the initial of a middle name like charvisdjarvis (assuming the name is "Charvis D. Jarvis".)?