I am very new in CakePHP
and I am trying to append the language to the URL in case the user type:
mydomain(dot)com/users
Then the Url has to change to http://mydomain.com/eng/users
Well, I am using Translate behavior and in my routes.php file I have :
Router::connect('/:lang/:controller/*', array(), array('lang' => '[a-z]{3}'));
Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
Ok so far everything works perfectly.
But when in my contorllers/AppController.php I try to append the url I get an error
This is what I have in my afterFilter function:
if (empty($this->params['lang']) ){
//Redirect to a language url
$this->redirect(array(
'lang'=> 'eng',
'controller' => $this->params['controller'],
'action' => $this->params['action'])
);
}
This works but when I try to go to the http:/mydomain.com/users/View/4
in the url I can see http:/mydomain(dot)com/eng/users/View/4
but what I see in the browaer is http:/mydomain(dot)com/users/View/
My question for you would be : What is the best way to achieve what I need ?
There is a plugin for doing this? or how can I handle this in CakePHP?
Well The problem was in my router.php
I had --->
Router::connect('/:lang/:controller/', array(), array('lang' => '[a-z]{3}'));
Router::connect('/:lang/:controller/:action/', array(), array('lang' => '[a-z]{3}'));
And the right way is ----->
Router::connect('/:lang/:controller/:action/', array(), array('lang' => '[a-z]{3}')); Router::connect('/:lang/:controller/', array(), array('lang' => '[a-z]{3}'));
Related
Is there any elegant way to redirect to an external url with parameters in laravel?
I'm using this code:
$redirect = redirect(config('app.paypal.url') .'?'. http_build_query([
'charset' => 'utf-8',
'paymentaction' => 'sale',
'no_note' => 1,
...
]));
but would prefer to use something like this (it doesn't work because the route is not defined):
$redirect = redirect(route(config('app.paypal.url'), [
'charset' => 'utf-8',
'paymentaction' => 'sale',
'no_note' => 1,
...
]));
You can do something like:
return redirect()->away('https://my.url.com')->with('user',$user)
->with('pass_code',$pass_code)
->with('amount',$amount)
->with('hash_value',$hash_value);
Define the url you want to redirect in $url
Then just use
return Redirect::away($url);
Here is the simple example
return Redirect::away('http://www.google.com?q=lorem+ipsum');
Docs
I'm trying to redirect my site to an external website with parameters. However when I do this, I get an Error Exception saying "Header may not contain more than a single header, new line detected". This would be my redirection code;
return Redirect::to($redirectUrl)->with(['ord_date' => $dueDate, 'ord_totalamt' => $cart_total, 'ord_gstamt' => 0.00, 'ord_shipname' => $user['name'],'ord_mercref' => $ord_mercref, 'ord_mercID' => $merchantid, 'ord_returnURL' => 'http://local.site.com/order/status', 'merchant_hashvalue' => $key]);
Note that I've tried using Redirect::away as well, and it results in the same error.
What am I doing wrong here?
Edit #1;
My $redirecturl is as such; $redirecturl = 'https://myurl.com" so it is in one line.
You should build the data into the $redirectUrl yourself instead of using with.
$query = http_build_query([
'ord_date' => $dueDate,
'ord_totalamt' => $cart_total,
'ord_gstamt' => 0.00,
'ord_shipname' => $user['name'],
'ord_mercref' => $ord_mercref,
'ord_mercID' => $merchantid,
'ord_returnURL' => 'http://local.site.com/order/status',
'merchant_hashvalue' => $key
])
$formattedRedirectUrl = preg_replace('/[ \t]+/', ' ', preg_replace('/[\r\n]+/', "\n", $redirectUrl);
return Redirect::to($formattedRedirectUrl.'?'.$query);
Hi I have tried various things but not sure if I am trying to over complicate things. Quite simply I want to add a small image country flag for the word 'Spanish' and 'English' and have an 'Alt' tag with those words instead. The following works fine with just text and as is but it would be cool to have the image either to replace or sit side by side with the word. Any help available?
echo $this->Html->link('Spanish', array('controller'=>'settings', 'action'=>'lang', 'spa'),
array('rel'=>'nofollow'));
}
else {
echo $this->Html->link('English', array('controller'=>'settings', 'action'=>'lang', 'eng'),
array('rel'=>'nofollow'));
}
You can either output just a linked image using the image method with a url attribute:-
<?php
echo $this->Html->image(
'spain.jpg',
array(
'alt' => 'Spanish',
'url' => array('controller' => 'settings', 'action' => 'lang', 'spa')
)
);
Or you can include an image with a text link by combining the normal CakePHP link method and the image method:-
<?php
echo $this->Html->link(
h('Spanish') . $this->Html->image('spain.jpg', array('alt' => 'Spanish')),
array('controller' => 'settings', 'action' => 'lang', 'spa'),
array('escape' => false, 'rel' => 'nofollow')
);
With the link method you need to remember to prevent Cake from escaping the img tag using 'escape' => false. If you disable escaping like this it is work remembering to escape any user provided text by using the h() method to prevent any HTML injection (I've shown this in my example wrapping the word 'Spanish' but this is only necessary if this is coming from a variable).
if you are using cake2 see this link to the cookbook
echo $this->Html->image("spain.jpg", array(
"alt" => "Spanish language",
'url' => array('controller' => 'settings', 'action' => 'lang', 'spa')
));
I'm building a user panel, and having some problems with data validation. As an example, the page where you change your password (custom validation rule comparing string from two fields (password, confirm password)):
Route:
Router::connect('/profile/password', array('controller' => 'users', 'action' => 'profile_password'));
Controller:
function profile_password()
{
$this->User->setValidation('password'); // using the Multivalidatable behaviour
$this->User->id = $this->Session->read('Auth.User.id');
if (empty($this->data))
{
$this->data = $this->User->read();
} else {
$this->data['User']['password'] = $this->Auth->password($this->data['User']['password_change']);
if ($this->User->save($this->data))
{
$this->Session->setFlash('Edytowano hasło.', 'default', array('class' => 'success'));
$this->redirect(array('action' => 'profile'));
}
}
}
The problem is, that when I get to http://website.com/profile/password and mistype in one of the fields, the script goes back to http://website.com/users/profile_password/5 (5 being current logged users' id). When I type it correctly then it works, but I don't really want the address to change.
It seems that routes aren't supported by validation... (?) I'm using Cake 1.3 by the way.
Any help would be appreciated,
Paul
EDIT 1:
Changing the view from:
echo $form->create(
'User',
array(
'url' => array('controller' => 'users', 'action' => 'profile_password'),
'inputDefaults' => array('autocomplete' => 'off')
)
);
to:
echo $form->create(
'User',
array(
'url' => '/profile/password',
'inputDefaults' => array('autocomplete' => 'off')
)
);
does seem to do the trick, but that's not ideal.
Check the URL of the form in the profile_password.ctp view file.
Try the following code:
echo $this->Form->create('User', array('url' => array('controller' => 'users', 'action' => 'profile_password')));
Also, I think your form might be a little vulnerable. Try using Firebug or something similar to POST a data[User][id] to your action. If I'm right, you should be setting:
$this->data['User']['id'] = $this->Auth->user('id');
instead of:
$this->User->id = $this->Session->read('Auth.User.id');
because your id field is set in $this->data.
HTH.
I am trying to run the remote function in the code below every 5 seconds. It runs only once if I remove the frequency option. I tried remoteTimer function, but when I use remoteTimer function, some code of the script goes outside the script tags and I see that in the webpage.
echo $ajax->form(array('type' => 'post', 'options' => array('model' => 'Thing',
'url' => array('controller' => 'things', 'action' => 'xyz'),'update' => 'dy4', 'indicator' => 'ldng', 'loading' => ( $ajax->
remoteFunction(array('url' => array('controller' => 'stories', 'action' =>
'keep'), 'update' => 'dy3', 'frequency' => 5))))));echo $form->input('a', array('type' => 'checkbox'));echo $form->input('b', array('type' => 'checkbox')); echo $form->end('RUN');
If in case, this cannot be done using CakePHP helpers, how can I do it with JavaScript?
Use remoteTimer in a very simple controller. See the page source and between <script> tags you'll get code that does what you want.You can use that code as value for "loading" option.