How can I use $html->link from within an element?
Thanks,
Tee
In both 1.2 and 1.3,this should work:
echo $html->link('linkname',array('controller'=>'somecontroller','action'=>'someaction/somearguments'));
Update
The html helper has changed a little in version 2.x.An example from the cook book
echo $this->Html->link('Enter', '/pages/home', array('class' => 'button', 'target' => '_blank'));
The new version of Cakephp has easier ways to work with html->links, definitely you can convert links into buttons.
If you want using bootstrap or some css however for normal links you can use:
<?= $this->Html->link ('Sometexthere', ['controller' =>'myController', 'action' =>'myAction']); ?>
Related
I just started using CakePHP, version 3. What I liked to start with is creating a menu based on a database table.
For creating a menu, I have seen that it should be placed inside the Element folder and called in 'default.ctp'. No problems here.
The problem is: How can I retrieve the data from the table while I'm not working in the controller, using the power of CakePHP? I can't find an example or something.
Inside 'Element/Menus/main.ctp' I do something like this (I know the link is not correct):
<?php
$menus = $this->requestAction('/Menus/index');
foreach ($menus as $menu) {
?>
<li><?= $this->Html->link('Menu', ['controller' => 'Menus', 'action' => 'index', '_full' => true]); ?></li>
<?php
}
?>
I try to get the data by calling 'requestAction' but it doesn't seem to be the right way.
Seems simple but I can't figure it out. Every link on my website suddenly shows the path behind the link
Example:
Not a member yet? Please register(/users/register) to add your pitch.
I can't select the path with Firebug to trace where it is coming from. Anyone has an idea? Thanks
UPDATE:
The PHP code for this link is:
echo $html->link('register', array('controller' => 'Users', 'action'=>'register'));
I use the BluePrint CSS Framework
Your problem isn't the style rule. Your problem is that you are linking a print only stylesheet from blueprint as a normal (screen, projection) stylesheet.
You need to make sure your css link has 'media' = 'print' as one of it's attributes.
IE
<?php
....
echo $this->Html->css( array(
join( DS, array( 'blueprint', 'print' )),
'stylesheet',
array( 'media' => 'print' )
);
...
?>
Somewhere in the head section of your layout.
try this...
$this->Html->link(__('register', true), array('controller' => 'Users', 'action' => 'register'));
cakePHP v. 1.3
I am having a problem using $html->link helper in my view. Consider this snippet...
/views/nodes/packages.ctp
<li><?php echo $html->link( "Package 1", array( "packages", "package1" ) )?></li>
Now when I click on the link, the address in address bar appears like
http://www.server.com/nodes/packages/packages/package1
Why is this happening? I haven't changed anything in my default routing configuration file.
Regards
Vikram
Let me answer this myself...
<li><?php echo $html->link( "Package 1", array( "controller" => "packages", "action" => "package1" ) )?></li>
I forgot to write the keys "controller" and "action". Silly me!
Regards
I am very new in cake php, i want to know how to create form in cake php,please describe,when we go to create a form then what i have to do,like create model and controller everything
In the view file, something like this would work:
<?php
echo $this->Form->create();
echo $this->Form->input('firstname', array('label' => 'Enter your first name:'));
echo $this->Form->input('email', array('label' => 'Enter your email address:'));
echo $this->Form->input('password', array('label' => 'Enter your password:'));
echo $this->Form->end('Save');
?>
In your controller:
if($this->request->is('post')){
$this->User->save( $this->request->data );
}
You can apply some sort of validation in your model, look in the documentation for that.
Best option to learn about cakephp is it's own doc book
But I'm providing you some basic code to create form :
$this->Form->create('ModelName');
$this->Form->input('ModelName.fieldname', array('type'=>'text', 'label'=>'Modified-Name'));
$this->Form->end(__('Submit'));
Here array('type'=>'text'...): type shows which type of input field you want.
(...'label'=>'Modified-Name'): By default it shows field text as fieldname but by using 'label' you can modify your field text.
$this->form->create('controlpage',
array(
'action'=>'controll',
'class'=>'class',
'enctype' => 'multipart/form-data',
'onsubmit'=>'return valid()'
));
Block quote
Create form in html save it as ctp
Block quote
And call it in view. enter code hereUse cake php book to read further.
I'm using this code now
echo $form->input('username');
How do I make sure the label shows up on a different line than the input field?
I managed to imitate what I'm trying to do, just want to make sure that I'm using it the right way.
echo $form->label('username', 'Username');
echo $form->input('username', array('label' => false));
Thanks,
Tee
The core of your request is putting a line-break between the <label> and <input> tags created by the FormHelper::input method. You can accomplish this in several ways. Probably the simplest option is the following:
echo $form->input('User.username', array('between'=>'<br />'));
Or you could also use a pure CSS solution, something like:
<style type="text/css">
div.input label { display: block; }
</style>
<?php echo $form->input('User.username'); ?>
This second option would leave you with cleaner PHP in your views, at the cost of more potential layout/stylesheet headaches.
Try this out.
<p>Username</p>
<?php echo $form->input('username', array('div' => false, 'label' => false)) ?>