Here's my current code within an index.phtml viewscript:
<?= $this->paginationControl($posts,'Sliding','application/partial/paginator', ['route' => 'home','lang'=>'it']); ?>
I'd like to pass the :lang parameter within this paginationControl call so that way the router is notified and the html results show the it lang inside the pagination html ahref code for clickable links.
I'm not quite sure how to correctly do this.
Here's my route:
'home' => [
'type' => Segment::class,
'options' => [
'route' => '/:lang',
'defaults' => [
'controller' => ApplicationIndexController::class,
'action' => 'index',
'lang' => 'en'
],
],
],
The resulting html from this paginator will show:
/pp/public/it?page=2
But it currently shows
/pp/public/en?page=2
even when im on the italian version of the page
Well it depends on how you setup your paginationControl partial or viewpage script.
The paginationControl parameters:
PaginationControl::__invoke(Paginator $myPaginator, $scrollingStyle, $partial, $params);
So within your partial or viewpage script for the pagination you are able to access all the stuff you passed to the $params parameter, like you would with your parameters you pass from your controller to your view pages or the partial viewhelper.
You could pass a parameter to the partial like the route to use and its route and query parameters.
$this->paginationControl(
$posts,
'sliding',
'application/partial/pagination',
[
'route' => 'home',
'routeParams' => ['lang' => 'it'],
'queryParams' => []
]
);
So now within your pagination partial you could use the route, routeParams and queryParams - Template used - Item pagination.
<?php
if (!isset($queryParams)) {
$queryParams = [];
}
if (!isset($routeParams)) {
$routeParams = [];
}
?>
<?php if ($this->pageCount): ?>
<div class="paginationControl">
<?= $this->firstItemNumber; ?> - <?= $this->lastItemNumber; ?>
<?= $this->translate('of'); ?> <?= $this->totalItemCount; ?>
<!-- First page link -->
<?php if (isset($this->previous)): ?>
<a href="<?= $this->url(
$this->route,
$routeParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->first]])
); ?>">
<?= $this->translate('First'); ?>
</a> |
<?php else: ?>
<span class="disabled"><?= $this->translate('First') ?></span> |
<?php endif; ?>
<!-- Previous page link -->
<?php if (isset($this->previous)): ?>
<a href="<?= $this->url(
$this->route,
$queryParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->previous]])
); ?>">
< <?= $this->translate('Previous') ?>
</a> |
<?php else: ?>
<span class="disabled">< <?= $this->translate('Previous') ?></span> |
<?php endif; ?>
<!-- Next page link -->
<?php if (isset($this->next)): ?>
<a href="<?= $this->url(
$this->route,
$routeParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->next]])
); ?>">
<?= $this->translate('Next') ?> >
</a> |
<?php else: ?>
<span class="disabled"><?= $this->translate('Next') ?> ></span> |
<?php endif; ?>
<!-- Last page link -->
<?php if (isset($this->next)): ?>
<a href="<?= $this->url(
$this->route,
$routeParams,
ArrayUtils::merge($queryParams, ['query' => ['page' => $this->last]])
); ?>">
<?= $this->translate('Last') ?>
</a>
<?php else: ?>
<span class="disabled"><?= $this->translate('Last') ?></span>
<?php endif; ?>
Related
I am new to Cakephp 3.0 i have created a form
<?php echo $this->Form->create('Login', array('url' => array('controller' => 'Login', 'action' => 'dashboard'))); ?>
<label for="login-username">username</label>
<?= $this->Form->input('username'); ?>
<label for="login-password">password</label>
<?= $this->Form->input('password', array('type'=>'password')); ?>
<?= $this->Form->submit('Login',array('class' => 'button round blue image-right ic-right-arrow')); ?>
<?= $this->Form->end() ?>
I have to redirect it to Login controller and dashboard action, but in inspect element i can see /stock_mgmt_system/login/dashboard where stock_mgmt_system is the project name.
Please try to solve my issue.
Did you named your controller LoginsController respecting cake convention ?
If yes just replace "login" with "logins" in "controller" just like that
echo $this->Form->create(null, [
'url' => ['controller' => 'Logins', 'action' => 'dashboard']
]);
https://book.cakephp.org/3.0/fr/views/helpers/form.html
In my yii application i have a simple form that contains two fields like this:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'message-form',
'enableClientValidation' => true,
//'enableAjaxValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
'validateOnType' => false,
'errorCssClass' => 'has-error',
'successCssClass' => 'has-success',
)));
?>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<?php echo $form->labelEx($model, 'subject'); ?>
<?php echo $form->textField($model, 'subject', array('class' => 'form-control')); ?>
<?php echo $form->error($model, 'subject', array('class' => 'alert alert-danger')); ?>
</div>
<div class="col-md-5"></div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-md-6">
<?php echo $form->labelEx($model, 'message'); ?>
<?php echo $form->textField($model, 'message', array('class' => 'form-control', 'id'=>'message')); ?>
<?php echo $form->error($model, 'message', array('class' => 'alert alert-danger')); ?>
</div>
</div>
</div>
......
//remaining of code
"subject" and "message" are two fields of "Notification" model. in Notification model, i have defined this rule:
array('subject, message', 'required')
my problem is validation for "subject" works but validation for "message" not works!
after submitting the form, error message of "subject" shows up but "message" does not have any error. can anyone help me solve this problem?
I use Yii 1.1.15
in http://www.yiiframework.com/doc/api/1.1/CActiveForm u can see example
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstName'),
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'firstName'); ?>
<?php echo $form->textField($model,'firstName'); ?>
<?php echo $form->error($model,'firstName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastName'); ?>
<?php echo $form->textField($model,'lastName'); ?>
<?php echo $form->error($model,'lastName'); ?>
</div>
<?php $this->endWidget(); ?>
i think u set enableAjaxValidation = true
Is there a reason for HTML attribute "id" => "message" on your message field? Yii CActiveForm generate a specific HTML id : modelName_fieldName for each field.
You don't need to set id manually (like your subject field; it don't have id). Yii needs those format of ids for validations, get/post data and more things (as client/js functions).
Remove "id" => "message" on your message field and test again.
I want to pass a php variable to modal window , what i am doing is opening a modal window using this link , but i want to pass a variable to this link and get same variable in modal window , i try to to do this to append a text in some div but it return html that i am unable to get in query
echo CHtml::link(
'Set Recipe', '', array(
'class' => 'testclass',
'id' => $finalDate,
'data-toggle' => 'modal',
'data-target' => '#myModal',
'fahadVar' => $finalDate
));
and when i click this button i got modal window how to get variable set in button
Here is simple modal code of yiibooster
<div class="modal-body">
<p>One fine body...</p>
</div>
<div class="modal-footer">
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'type' => 'primary',
'label' => 'Save changes',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
<?php $this->widget(
'bootstrap.widgets.TbButton',
array(
'label' => 'Close',
'url' => '#',
'htmlOptions' => array('data-dismiss' => 'modal'),
)
); ?>
</div>
<?php $this->endWidget(); ?>
thanks in advance
You should create a Widget.
Note: I copied below from another post. It works like charm.
First Create a new widget. Let say the name is CategoryWidget. Put this widget under components directory protected/components.
class CategoryWidget extends CWidget {
public function run() {
$models = Category::model()->findAll();
$this->render('category', array(
'models'=>$models
));
}
}
Then create a view for this widget. The file name is category.php. Put it under protected/components/views
category.php
<?php if($models != null): ?>
<ul>
<?php foreach($models as $model): ?>
<li><?php echo $model->name; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Then call this widget from your main layout.
main.php
// your code ...
<?php $this->widget('CategoryWidget') ?>
I've used this method on a few website builds with no obvious problems. But I'm wondering if it's an okay/proper way to utilize PHP variables as website navigation/page indicators so that site-wide changes can be easily managed in 1 file (upper.php)? Are there any drawbacks? Maybe too many requests to the server, load times, improper coding or negative S.E.O. ramifications?
I've omitted surrounding code such as < body >, etc. for simplicity and indicated where more code would be with "..."
index.php
<?php $urhere="home"; include ("upper.php"); ?>
<div>This is the Home page content</div>
...
about.php
<?php $urhere="about"; include ("upper.php"); ?>
<div>This is the About page content</div>
...
contact.php
<?php $urhere="contact"; include ("upper.php"); ?>
<div>This is the Contact page content</div>
...
upper.php
...
<meta name="description" content="
<?php
if ($urhere=="home") echo "Home page description";
if ($urhere=="about") echo "About page description";
if ($urhere=="contact") echo "Contact page description";
?>
...
<title>
<?php
if ($urhere=="home") echo "Home page Title";
if ($urhere=="about") echo "About page Title";
if ($urhere=="contact") echo "Contact page Title";
?>
</title>
...
<div id="nav">
<ul>
<li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
<li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
<li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
</ul>
</div>
I see some serious maintenance problems with this approach.
Every time you add a new page, you have to add three (for now) if-statements.
upper.php knows too much about other pages -- their window titles and metadata.
I would suggest that instead of using the $urhere variable to make all the decisions in upper.php, you set variables, like window title and metadata, which can be directly used by upper.php. Here's an example:
upper.php
<meta name="description" content="<?php echo $description ?>"/>
...
<title><?php echo $title ?></title>
...
<?php
$pageTypeToLinkMap = array(
'home' => array(
'url' => '/home',
'linkText' => 'Home'
),
'about' => array(
'url' => '/about',
'linkText' => 'About',
'sublinks' => array(
'who-we-are' => array(
'url' => '/who-we-are',
'linkText' => 'Who We Are'
),
'what-we-do' => array(
'url' => '/what-we-do',
'linkText' => 'What We Do',
'sublinks' => array(
'business' => array(
'url' => '/business',
'linkText' => 'Business'
),
'technology' => array(
'url' => '/technology',
'linkText' => 'Technology'
)
)
)
)
),
'contact' => array(
'url' => '/contact',
'linkText' => 'Contact'
)
);
function getLinkElement($pageType, array $linkData)
{
$class = $urhere == $pageType ? 'urhere' : '';
$anchorElement = "$linkData[linkText]";
if (isset($linkData['sublinks']))
{
$sublinkElements = array();
foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
$sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);
$sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
}
else
$sublinksListElement = '';
return "<li>$anchorElement$sublinksListElement</li>";
}
?>
<div id="nav">
<ul>
<?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
</ul>
</div>
index.php
<?php
$pageTitle = "Home";
$description = "This is home.";
$urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
?>
<div>This is the Home page content</div>
about.php
<?php
$pageTitle = "About";
$description = "About us";
$urhere = "about";
?>
<div>This is the About page content</div>
EDIT
In response to your comment, here's how you can improve your upper.php while keeping the same original structure:
upper.php
<?php
$titles = array(
'home' => 'Home page Title',
'about' => 'About page Title',
'contact' => 'Contact page Title'
);
$descriptions = array(
'home' => 'Home page description',
'about' => 'About page description',
'contact' => 'Contact page description'
);
$linkClasses = array_fill_keys(array_keys($titles), '');
$linkClasses[$urhere] = 'urhere';
?>
...
<meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>
...
<title><?php echo $titles[$urhere] ?></title>
...
<div id="nav">
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
There you go, if-less code.
There is no right or wrong way to build a webpage, and there are many options. In general, I like to use a templating system of some type, which makes the design of the page easy to seperate from the business logic behind it.
You could try something like Smarty (http://www.smarty.net/crash_course).
Check out some of the awesome MVC PHP Frameworks. There is a good comparison of the MVC PHP frameworks at the site below
http://jonathanmh.com/best-php-mvc-frameworks-of-2013/
I have a login form in my header that talks to my users_controller but the form itself isn't in a view being generated by the users controller so I get two problems
1.) The password field doesn't get treat like a password field and is just a normal text field
2.) When submitting the form it just redirects to the login action
Here is the code and it's used in BOTH the login view and in my header (so I know it works):
<?php echo $this->Form->create(null, array('id' => 'loginform', 'type' => 'post',
'url' => array('controller' => 'users', 'action' => 'login'))); ?>
<fieldset id="login">
<ul class="clearfix">
<li id="li-username">
<?php echo $this->Form->input('username', array('label'=>false,'placeholder'=>'Username or email address')); ?>
</li>
<li id="li-password">
<?php echo $this->Form->input('password', array('label'=>false,'placeholder'=>'Password')); ?>
<span id="iforgot"><?php echo $this->Html->link('?',
array('controller' => 'users', 'action' => 'forgotpassword'), array('title' => 'Forgot your password?')); ?></span>
</li>
<li id="li-submit">
<button type="submit" title="Log in">Log in ►</button>
</li>
</ul>
</fieldset>
<?php echo $this->Form->end(); ?>
Why not changing
Form->create(null,...
to
Form->create('User',...
otherwise change
Form->input('username',...
to
Form->input('User.username',...
You can use type = 'password'
echo $this->Form->input('password', array('label'=>false, 'type' => 'password', 'placeholder'=>'Password'));
You have specified array('controller' => 'users', 'action' => 'login') in your $this->Form->create() statement. So the form is submitted to "/users/login"
If you don't want to submit the form to '/users/login', you can use AJAX to perform login.
Hope these will answer your queries.