cakePHP passing URLs as a param - url

I have an ImportController with a function admin_getcontents.
function admin_getcontents($url = null)
{
$contents = json_decode(file_get_contents(($url)),true);
//some stuff
}
Through ajax I call /admin/import/getcontents/ with:
$.get('/admin/import/getcontents/'+ encodeURIComponent($('#urlcheck').val()) ,function(data) {
$('#importtable').html(data);
$('#busy-indicator').fadeOut('high');
});
so I call the page: /admin/import/getcontents/http%3A%2F%2Flocalhost%2Feclipse%2Fanarxeio%2Fexport%2Fcontents%2F
Apache gives me an 404 error. If I call /admin/import/getcontents/1 the page loads correctly. So I figured to pass a ? before the parameter like:
admin/import/getcontents/?http%3A%2F%2Flocalhost%2Feclipse%2Fanarxeio%2Fexport%2Fcontents%2F
Now I don't get a 404 error but $url param in admin_getcontents() is empty. How can I achieve the above?
Thanks

A quick fix would be to triple url encode your url:
// javascript
$.get('/admin/import/getcontents/'+ encodeURIComponent(encodeURIComponent(encodeURIComponent($('#urlcheck').val()))) ,function(data) {
$('#importtable').html(data);
$('#busy-indicator').fadeOut('high');
});
Then url decode it in your php before you use it:
// php
function admin_getcontents($url = null)
{
$url = urldecode(urldecode($url));
$contents = json_decode(file_get_contents(($url)),true);
//some stuff
}
EDIT following comments:
You will need to set up your routing to pass the url parameter. Looking at your setup, it should looking something like:
Router::connect('/admin/import/getcontents/:url', array(
'controller' => 'import',
'action' => 'getcontents',
'admin' => true),
array(
'url' => '(.*)',
'pass' => array('url')
)
);

Related

CakePHP login with URL

i want to provide the possibility to login on my website by passing the username and password in the url. Something like this:
http://vabdat.local/rest/bpid/34.xml?User=Mustermann&password=abcdef123
But i have no idea how to solve that problem. I `m actually using CakePHP 2.x
public function beforeFilter() {
if(in_array(strtolower($this->params['controller']),array('rest'))){
$user = $this->Auth->identify($this->request, $this->response);
$this->Auth->login($user);
$this->Auth->authenticate = array('Basic' => array (
'fields' => array('User.username' => 'Username', 'User.password' => 'Password'),
'userModel' => 'User',
'passwordHasher' => 'Blowfish',
'scope' => array (
'User.active' => 1,
)
) );
$this->Security->unlockedActions = array();
}
else{
}
}
Can someone tell me how to find a solution to this?
Thanks a lot.

Parameters in CakePHP3 URL

Using CakePHP3, I have a search form with GET method. Trying to get the URL parameter seems not to work. I doing as follow :
if(isset($this->request->params['text'])){
// the code ...
}
The search form's action has a defined route :
$routes->connect('/search', [
'controller' => 'Top',
'action' => 'index'
],
[
'_name' => 'search'
]);
How to fix this ?
You can do something like this fo fetch querystring($_GET) parameters
if(isset($this->request->query['text'])){
// the code ...
}
I think you are looking for this:
$this->request->query('text');
Reference: http://book.cakephp.org/3.0/en/controllers/request-response.html#query-string-parameters

Basic use of TranslationServiceProvider in Silex

I'm trying to use the Silex TranslationServiceProvider in the most straighforward way i.e.
<?php
// web/index.php
require_once __DIR__.'/../vendor/autoload.php';
$app = new Silex\Application();
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale' => 'fr',
'locale_fallbacks' => array('en')
));
$app['translator.domains'] = array(
'messages' => array(
'en' => array('message_1' => 'Hello!'),
'fr' => array('message_1' => 'Bonjour')
));
echo $app['translator']->trans('message_1');
// I get 'Hello!' (why ?)
It seems that the 'locale' => 'fr' line when initializing the TranslationServiceProvider is not taken into account and that the only parameter that counts is locale_fallbacks (when I change locale_fallbacks to 'fr', the message is displayed in french)
Is there something very simple I am missing here ?
Thanks in advance
Edit
When I use the the setLocale function, it still doesn't work and seems to override the locale_fallbacks:
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'locale_fallbacks' => array('en')
));
$app['translator']->setLocale('fr');
echo $app['translator']->getLocale(); // returns 'fr' as expected
$app['translator.domains'] = array(
'messages' => array(
'en' => array('message_1' => 'Hello!'),
'fr' => array('message_1' => 'Bonjour')
));
echo $app['translator']->trans('message_1');
// now returns 'message_1' (??)
What's wrong with the way I use the provider ?
You must set the locale, otherwise the fallback is used:
$app['translator']->setLocale('fr');
I'm setting the locale in a $app->before() handler:
$app->before(function(Request $request) use ($app) {
// default language
$locale = 'en';
// quick and dirty ... try to detect the favorised language - to be improved!
if (!is_null($request->server->get('HTTP_ACCEPT_LANGUAGE'))) {
$langs = array();
// break up string into pieces (languages and q factors)
preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
$request->server->get('HTTP_ACCEPT_LANGUAGE'), $lang_parse);
if (count($lang_parse[1]) > 0) {
foreach ($lang_parse[1] as $lang) {
if (false === (strpos($lang, '-'))) {
// only the country sign like 'de'
$locale = strtolower($lang);
} else {
// perhaps something like 'de-DE'
$locale = strtolower(substr($lang, 0, strpos($lang, '-')));
}
break;
}
}
$app['translator']->setLocale($locale);
$app['monolog']->addDebug('Set locale to '.$locale);
}
});
Two remarks on Ralf’s answer:
Almost all that is done in the before() middleware in unnecessary, as the Request class provides a convenience method for determining the “best” language based on the “Accept-Language” header. So basically, nothing more than this is required:
$app->before(
function (Request $request) use ($app) {
$app['translator']->setLocale($request->getPreferredLanguage(['en', 'fr']));
}
);
Silex uses the “magic” variable “{_locale}” in a route’s definition to set the locale accordingly. This means you do neither need to declare the locale when instantiating TranslationServiceProvider, nor call setLocale(), but simply declare the route like this:
$app->get('/{_locale}/path', ...);
Now, Twig and $app['translator'] (inside the Closure or controller method) will automatically be set to the correct locale.

Url helper with GET parameter in laravel

I want to create an url with this format :
domain.com/list?sortBy=number&sortDir=desc
in my View (blade). I'm using this approach which I don't really prefered :
{{ url("list")."?sortBy=".$sortBy."&sortDir=".$sortDir }}
because using
{{ url("list", $parameters = array('sortBy' => $sortBy, 'sortDir' => $sortDir) }}
didn't produce as I hoped. Is there a better way?
Have you tried the URL::route method.
Example Route:
Route::get('/list', array('as' => 'list.index', 'uses' => 'ListController#getIndex'));
Retrieve the URL to a specific route with query string:
URL::route('list.index', array(
'sortBy' => $sortBy,
'sortDir' => $sortDir
));
If your using a closure on the route:
Route::get('/list', array('as' => 'list.index', function()
{
return URL::route('account-home', array(
'sortBy' => 1,
'sortDir' => 2
));
}));

Cakephp Routes And General Routing

I have a controller with an action and a variable like this:
class AccountsController extends AppController
{
function profile($username = null)
{
}
}
The url for this page is:
[domain]/accounts/profile/[username]
How do I make it:
[domain]/[username]
?
Try:
//in your routes.php file
Router::connect('/:username',
array('controller' => 'accounts', 'action' => 'profile'),
array(
'pass' => array('username')
)
);
Hope it helps

Resources