How to get url without pagination YII - yii

this code takes url from page and display it
http://site<?= \Yii::$app->request->getUrl() ?>
But he takes it with pagination, like that:
site/category/page?p=2.
i need to take this url, but without pagination, only url, like that:
site/category/page
help me please. I think to use suffix, but idk hot it works if url didn`t have pagination

Add the below code to your UrlManager inside config/main.php
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),

Related

Yii Appending base Url to href

I have external links on my webpage:
<a target="_blank" rel="nofollow" href=<?php echo $r['url'] ;?>>
VISIT STORE
</a>
When $r['url'] has http:// it shows the correct external url but when it has only www or just the website name with the domain it appends the web page's url.
Case 1 : url = http://google.com Works fine
Case 2: url = www.google.com creates a link as: http://localhost/appname/controller/action/www.google.com
Yii has no any relation to your problem. You don't know difference between absolute and relative URL's.
In your code, you don't use Yii anywhere. Yii has very powerfull URL manager with methods: createUrl, createAbsoluteUrl and other. You don't use this.
You need to understand the difference between absolute and relative URL's and your question gone away. There are more information in internet and in StackOverflow too: Absolute vs relative URLs
try this:
<?php echo CHtml::link('Google', '//www.google.com', array('target'=>'_blank')) ?>
Hope all your URLs are external. Then check your url for http:. If url contains http, then use directly, Otherwise add.
function is_valid_url($url)
{
if(strpos($url, "http://")!==false)
return $url; // correct
else return "http://$url";// http not found.
}
In your a tag
<a target="_blank" rel="nofollow" href=<?php echo is_valid_url($r['url']) ;?>>
VISIT STORE </a>

Move Buddypress Registration to index page

I have a custom index page. I need to have the buddypress registration on the index page. Something like facebook.
Can anyone help?
Thanks
You can simply copy paste the content of theme bp-default/registration/register.php into your page. You can copy everything that is there on register.php between these 2 lines:
<?php do_action( 'bp_before_register_page' ); ?>
and
<?php do_action( 'bp_after_register_page' ); ?>
But, but...
you need to remove this if clause
<?php if ( 'request-details' == bp_get_current_signup_step() ) : ?>
just remove the opening and closing if statement. Everything inbetween should stay as it is. But there's one problem, since you remove this if statement your registration form will always be visible. If you can't live with that then you need to do some workaround to fix this.

Yii - urlmanager and active highlighting menus

I've just figured out how to configure the urlmanager rules to work with rules such as
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<type:\w+>s'=>'article/index',
)
however, now my CMenu does not have active highlighting for my main menu with link
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Articles', 'url'=>array('/articles')),
)); ?>
Can this active highlighting still work even though I want my URLs to look like '/articles'?
I've found a solution, seems kinda gross though. All I did was add an 'active' criteria to check if the current URI has '/article' as the front.
array('label'=>'Articles',
'url'=>array('/articles'),
'active'=>strpos(Yii::app()->request->requestUri, '/article')===0),
array('label'=>'Articles',
'url'=>array('/articles'),
'active'=>Yii::app()->controller->name == 'articles'),

Special characters in Yii framework menu

I'm developing a CMS using Yii Framework. In developing the theme I have a problem. In the menus I have some special characters (The website is in Italian). the HTML markups for special characters doesn't work for menu items. And if I put the character itself it looks different. Here is my code:
<div class="horizontal-menu">
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'LE ATTIVITÀ', 'url'=>array('/site/page', 'view'=>'attivita')),
array('label'=>'NEWS', 'url'=>array('/site/page', 'view'=>'news')),
),
)); ?>
LE ATTIVITÀ is the one making the problem. How can I show special characters here?
CMenu's labels are HTML-encoded by default, so you should use array('label'=>'LE ATTIVITÀ', 'url'=>array('/site/page', 'view'=>'attivita')),.
However if you must use HTML-encoded strings in your code, you can turn the encoding off by setting the encodeLabel to false, like this $this->widget('zii.widgets.CMenu',array(
'items'=>array(),
'encodeLabel'=>false,
),

CHtml::link - how to add a html class?

On docs we can read:
public static string link(string $text, mixed $url='#', array $htmlOptions=array ( ))
Question:
I don't understand what $htmlOptions means here. I don't understand how to pass from this representation to a real code.
Can anyone please provide an example about how can we generate a link with a class defined.
Something like:
link hello
It's easier than you might think, although Yii's documentation is perhaps a bit more convoluted than needs to be. However, it does say that $htmlOptions is
additional HTML attributes. Besides normal HTML attributes, a few
special attributes are also recognized (see clientChange and tag for
more details.)
In essence, whatever key/value pairs you put into the array will come out as HTML attributes¹. So, what you want to do is
CHtml::link('link hello', '#', array('class' => 'hello'));
¹except the "special" values that the docs refer to, which will not end up rendered in HTML as-is but either modify the way link works slightly, or end up affecting the HTML in other ways.
<?php echo CHtml::link('Link Text',array('controller/action','param1'=>'value1'), array('target'=>'_blank','class'=>'hello'); ?>
It will be shown as below.
<!--if you disabled url manager in "protected/config/main.php" the output will be -->
<a target="_blank" class="hello" href="index.php?r=controller/action&param1=value1">Link Text</a>
<!--if you enabled url manager in "protected/config/main.php" the output will be -->
<a target="_blank" class="hello" href="controller/action/param1/value1">Link Text</a>
To get a detailed description about CHtml in yii Check this link.
<?php echo CHtml::link("Label Text" , array("/controller_here/action_here") , array('class' => 'class_here')); ?>
or
<?php echo CHtml::link("Label Text" , Yii::app()->createUrl("/controller_here/action_here") , array('class' => 'class_here')); ?>