cakePHP 3 link inside email view - email

I have an issue build a link inside an email view, it doesn't out put the URL i'm passing from the controller for some reason..
the URL string that i'm passing to the view is missing the http://domain prefix
some action in usersController:
$email = new Email(['gmail','transport'=>'gmail']);
$email->template('recover', 'default')
->emailFormat('html')
->viewVars([
'username' => $user['username'],
'url' => '/users/resetpwd/u/'.$user['username'].'/t/'.$user['token']
])
->from(['mailer#domain.com' => 'My Site'])
->to('example#domain.com')
->subject('Password recovery')
->send()
recover.ctp:
<p>Welcome <b><?= $username; ?></b>,<br/>
you requested a password change.<br/>
To set a new password, please: <?php echo $this->Html->link('Click Here', $url, ['_full' => true,'escape' => true]); ?></p>
<?= $this->Html->image('banniere.gif', array('fullBase' => true));?><br/>
<p>This email was sent from My Site</p>

If you want to ensure that the URL is always a "full" one, then the proper solution here is Router::url(). That way you could even declare the URL in array format (which should normally be the preferred style anyways) without breaking things.
use Cake\Routing\Router;
// ...
<?= $this->Html->link('Click Here', Router::url($url, true), ['escape' => true]); ?>

Related

can't pass parameter from view to controller

Hi all I am currently working with zend 2.3. and while listing students I am trying to make link that allows to show detales about selected person. My problem is that I can't pass selected id to the action in module's controller. Here is my view's code that coresponds to link:
<a href="<?php echo $this->url('admin' ,
array( 'controller'=>'Admin', 'action'=>'viewstudent', 'ID' => $student->ID))."/admin/viewstudent";?>">detales</a>
and the action in controller
public function viewstudentAction ()
{
$id = (int) $this->params()->fromRoute('ID', 0);
echo $id;
//echo var_dump($id);
return new ViewModel(array(
'students' => $this->getStudentTable()->viewstudent($id),
));
}
Then I var_dump $id variable it shows 0 So what is the correct way to do this?
I have edited the a href's code like this
<a href="<?php echo $this->url('admin/default' ,
array( 'controller'=>'Admin', 'action'=>'viewstudent', 'id' => $student->ID));?>">Просмотр</a>
and it resolves to this:
Просмотр
url is
http://localhost:8080/disability/public/admin/Admin/viewstudent
The url is
http://localhost:8080/disability/public/admin/Admin/viewstudent
Problem is the same id didn't pass
I have found the solution to the problem. I passed the parameter using query here is how it looks in view file
<a href="<?php echo $this->url('admin/default',
array('controller' => 'Admin',
'action'=>'viewstudent'
),
array('query' =>
array('id' => $student->ID)
)
);
?>">view</a>
And then using $this->params retrieve it in my controller file
$id = $this->params()->fromQuery('id');

Show 404 error in fuelphp pagination

I want to use pagination in FuelPHP, I'm using this code:
$config = array(
'pagination_url' => 'http://localhost/live/public/index.php/admin/accounts/index/',
'total_items' => Model_Account::count(),
'per_page' => 10,
'uri_segment' => 3,
// or if you prefer pagination by query string
//'uri_segment' => 'page',
);
$pagination = Pagination::forge('mypagination', $config);
$data['example_data'] = Model_Account::query()
->rows_offset($pagination->per_page)
->rows_limit($pagination->offset)
->get();
// we pass the object, it will be rendered when echo'd in the view
$data['pagination'] = $pagination;
and here's the view:
<?php echo Pagination::instance('mypagination')->previous(); ?>
<?php echo Pagination::instance('mypagination')->render(); ?>
<?php echo Pagination::instance('mypagination')->next(); ?>
<?php echo Pagination::instance('mypagination')->last(); ?>
But when I try to click on any pagination in frontend then it sends error: 404 page not found
You will need to have a route set up so fuel knows how to map the URI admin/accounts/index/:page to the correct controller.
Something like the below should work in your routes.php config file. (Might need tweaking depending on your app)
'admin/accounts/index/:page' => 'admin/accounts/index'

CakePHP Text as Form Submit

I've searched the web and have come up with nothing. (Multiple search engines too - I have looked!)
I'm trying to have a text link as the 'form submit' button. Any ideas if this is possible in CakePHP?
Current view code below!
<?php
echo $this->Form->create('trainees', array(
'action' => 'reassign'
));
echo $this->Form->input('emailaddress', array(
'value' => 'scott#something',
'type' => 'hidden',
));
echo $this->Form->submit('Re-Assign Mentor', array(
'class' => 'submit mid',
'before' => '<p>',
'after' => '</p>'
));
echo $this->Form->end();
?>
You need to use the HtmlHelper to output a link. In it's simplest form you use the text you want displayed with the URL that it should link to. In this case it will be JavaScript:
$this->Html->link('Submit Form', 'javascript:document.forms["myform"].submit();');
There are two additional parameters (a $options array and $confirmMessage boolean), but they along with the URL are optional.
You can also call your own JavaScript function if you need to do client side verification and call the submit function from there (also verify on the server as clients can lie).
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link

How do I take over a value sent by a "null form" redirecting to another controller?

This is my form:
echo $this->Form->create(null, array('url' => '/offices/addOffice'));
echo $this->Form->input('id', array('type' => 'hidden',
'value' => $this->data['Agency']['id']));
echo $this->Form->end(__('Add Office', true));
Now in the addOffice function I would like to get the value sent from this form.
What I did is:
function addOffice($id = null){
$this->set(compact('id'));
}
but it doesn't send the $id to the view. What am I doing wrong?
I'm not sure what you're asking, because if you want to "get the value sent from this form", then "send the $id to the view" doesn't have anything to do with it.
If you want to retrieve the the data from the form, you shouldn't put null as the model's name. Use the relevant model, which I assume is "Office" in this case.
echo $this->Form->create('Office', array('url' => '/offices/addOffice'));
echo $this->Form->input('id', array('type' => 'hidden',
'value' => $this->data['Agency']['id']));
Now the id can be retrieved from $this->data[ 'Office' ][ 'id' ] in the controller.
If the question is how you can set the id in the first place using the function parameter, you have to use the $id parameter you've set in the controller:
echo $this->Form->input('id', array('type' => 'hidden',
'value' => $id));

Problem passing URL variables in post form submission with CakePHP FormHelper

I'm writing my first CakePHP application and am just writing the second part of a password reset form where a user has received an email containing a link to the site and when they click it they're asked to enter and confirm a new password.
The url of the page is like this:
/users/reset_password_confirm/23f9a5d7d1a2c952c01afacbefaba41a26062b17
The view is like:
<?php echo $form->create('User', array('action' => 'reset_password_confirm')); ?>
<?php
echo $form->input('password', array('label' => 'Password'));
echo $form->input('confirm_password', array('type' => 'password', 'label' => 'Confirm password'));
echo $form->hidden('static_hash');
?>
<?php echo $form->end('Reset password'); ?>
However this produces a form like:
<form id="UserResetPasswordConfirmForm" method="post" action="/users/reset_password_confirm/8">
The problem is the user id (8 in this case) is being added to the form action. It's not really a problem here, but when I want to pass through the hash to my controller:
function reset_password_confirm($static_hash=null) {
// function body
}
$static_hash is now populated with 8 rather than the hash from the URL.
I know I could sort this out by creating the form tag myself rather than using $form->create but is there a more cakey way of doing this?
$form->create('User', array('action' => '…', 'id' => false));
Just explicitly set params you don't want passed to null or false. This is unfortunately a case where Cake tries to be a little too intelligent for its own good. ;o)
You could probably also do something like this to POST to the same URL again:
$form->create('User', $this->here);
How about passing it as a parameter instead of form data :
<?php
echo $form->create('User', array('action' => 'reset_password_confirm', $static_hash));
echo $form->input('password', array('label' => 'Password'));
echo $form->input('confirm_password', array('type' => 'password', 'label' => 'Confirm password'));
echo $form->end('Reset password');
?>
and in the controller :
function reset_password_confirm($static_hash = null) {
// Check if form is submitted
if (!empty($this->data)) {
// if it submitted then do your logic
} else {
$this->set('static_hash', $static_hash); // Else, pass the hash to the view, so it can be passed again when form is submitted
}
}
Hope this help :)