Using default route parameters for non-required Kohana Route::url - kohana

I have a route setup that looks like this:
Route::set('my_route', 'r/<controller>(/<action>)(/(<name>-)<hash>)', array(
'controller' => '[a-z]+',
'action' => '[a-z]+',
'hash' => '\w{13}',
'name' => '[a-z]+',
))->defaults(array(
'directory' => 'my_dir',
'controller' => 'welcome',
'action' => 'index',
'name' => null,
));
Which works by itself. The problem comes in when I try and build a URL from the route, like this:
return Route::url('my_route', array(
'action' => 'test',
));
I get this error message:
Kohana_Exception [ 0 ]: Required route parameter not passed: name
So if I set name to null, I get the same result.
If I set name to false, there is no error message, but the urls look like this:
/r/welcome/test/-
notice the - on the end?
Now I could strip that off, but I'm hoping there’s a better way.

It seems to me that you are trying to fix your routing by using 1 route for all. This is not the right way to do things.
Just make multiple routes (maybe you have to tweak this):
Route::set('my_route', 'r/<controller>(/<action>)/(<name>-)<hash>', array(
'controller' => '[a-z]+',
'action' => '[a-z]+',
'hash' => '\w{13}',
'name' => '[a-z]+',
))->defaults(array(
'directory' => 'my_dir',
'controller' => 'welcome',
'action' => 'index',
'name' => null,
));
Route::set('my_route2', 'r/<controller>(/<action>)', array(
'controller' => '[a-z]+',
'action' => '[a-z]+',
'hash' => '\w{13}',
'name' => '[a-z]+',
))->defaults(array(
'directory' => 'my_dir',
'controller' => 'welcome',
'action' => 'index',
'name' => null,
));
Remember the route system is really powerful and more routes doesn't mean it gets slower. So just make as many clearly possible routes and don't try to run everything by 1 route.

Related

Pagination with rocksolid custom content elements

I created a custom rocksolid content element. Now I would like to have pagination for that content element in the frontend. Is it possible to have pagination to be configured with rocksolid custom content element. If so, how it is possible? My config file is
'inputType' => 'list',
'fields' => array(
'html' => array(
'label' => array(
'en' => array('Script code', ''),
'de' => array('Script code', ''),
),
'inputType' => 'text',
'eval' => array('mandatory'=>true, 'allowHtml'=>true, 'class'=>'monospace', 'rte'=>'ace|html', 'helpwizard'=>true),
),
'text' => array(
'label' => array(
'en' => array('Text', ''),
'de' => array('Text', ''),
),
'inputType' => 'textarea',
'eval' => array(
'rte' => 'tinyMCE',
'tl_class' => 'clr',
),
),
Is it possible to add the pagination configuration in this config file? If so what is the configuration?

Yii2 disable highlighting menu item

My main.php code
<?php
NavBar::begin([
'brandLabel' => 'Styl-dekoracje.pl',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'Orders', 'url' => ['/order']],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']],
],
]);
NavBar::end();
?>
When I click for login/logout or home item its will be highlight. But how can I disable highlighting for SiteController? Where is file who set item as active?
Each item insive Nav have active property.
Set it depending on current controller, action, or route.
Example:
[
'label' => 'Login',
'url' => ['/site/login'],
'active' => $this->context->route == 'site/login',
],
Setting this for site/logout doesn't make sense because it's immediate action with redirect.
Official documentation:
Nav $items
View $context
Controller $route
I am not familiar with Yii2, but in Yii there was an activeCssClass option.
'activeCssClass' => ''
The code above disbaled the higlighting of active menu item.
You can use Url::to() to make it work. This is just a workaround.
<?php
NavBar::begin([
'brandLabel' => 'Styl-dekoracje.pl',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'Home', 'url' => Url::to(['/site/index'])],
['label' => 'Orders', 'url' => Url::to(['/order'])],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => Url::to(['/site/login'])] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => Url::to(['/site/logout']),
'linkOptions' => ['data-method' => 'post']],
],
]);
NavBar::end();
?>

Exclude custom taxonomy

I have a custom post type 'listings' and one of its taxonomies is 'status'. I want to create two widgets:
display all 'listings' WITH 'status' 'sold'.
display all 'listings' WITHOUT 'status' 'sold'.
I've achieved the first widget using
query_posts( array(
'status' => 'sold' )
);
I can't create the second widget. It should be like "status => !sold", or exclude sold. Any ideas?
Try below code when you need status = sold
$args = array(
'post_type' => 'listing',
'meta_query' => array(
array(
'key' => 'status',
'value' => 'sold',
'compare' => 'LIKE'
)
)
);
$myQuery = new WP_Query($args);
And below code when you want status != sold
$args1 = array(
'post_type' => 'listing',
'meta_query' => array(
array(
'key' => 'status',
'value' => 'sold',
'compare' => 'NOT LIKE'
)
)
);
$myQuery1 = new WP_Query($args1);
This works perfectly...
query_posts( array(
'post_type' => 'listings',
'tax_query' => array(
array(
'taxonomy' => 'status',
'field' => 'slug',
'terms' => 'sold',
'operator' => 'NOT IN'
),
)
)
);
This code excludes status => sold from post_type => listings

How could I redirect an url with routes.php or htaccess?

How could I redirect an url with routes.php or htaccess?
My homepage:
teszt.hu/onecontroller/oneaction/
teszt.hu/categories/index/
And the redirect:
teszt.hu/specword/categories/index/ => teszt.hu/categories/index/?s=specword
teszt.hu/specword/onecontroller/oneaction/ => teszt.hu/onecontroller/oneaction/?s=specword
You should use CakePHP's Routes for this.
Read more about routes here: http://book.cakephp.org/2.0/en/development/routing.html
Off the top of my head, I think it would be something like this:
Router::connect(
'/specword/:controller/:action',
array('?' => array('s'=>'specword')),
);
$accWebpages = '(webpage1|webpage2|specword)';
//Router::connect('/:accwebpage/:controller/:action', array(), array('pass' => array('accwebpage'), 'accwebpage' => $accWebpages));
Router::connect('/:accwebpage/', array('controller' => 'homes', 'action' => 'index'), array('pass' => array('accwebpage'), 'accwebpage' => $accWebpages));
Router::connect('/kategoria/:page/:category-:slug', array('controller' => 'products', 'action' => 'index'), array('pass'=>array('page', 'category', 'slug'), 'page' => '[0-9]+', 'category' => '[0-9]+'));
Router::connect('/:accwebpage/kategoria/:page/:category-:slug', array('controller' => 'products', 'action' => 'index'), array('pass'=>array('page', 'category', 'slug', 'accwebpage'), 'page' => '[0-9]+', 'category' => '[0-9]+', 'accwebpage' => $accWebpages));
...

How Kohana implement sessions saved to database?

the source code hands Session_Cookie and Session_Native class but the Session_Database,
here the config file
<?php defined('SYSPATH') or die('No direct script access.');
return array(
'database' => array(
'database' => array(
'name' => 'blog_session_cookie',
'encrypted' => TRUE,
'lifetime' => 43200,
'group' => 'default',
'table' => 'sessions',
'columns' => array(
'session_id' => 'session_id',
'last_active' => 'last_active',
'contents' => 'contents'
),
'gc' => 500,
),
),
);
usage
$session = Session::Instance("Database");
$session->set('username', 'far');
great, its added a column in database, amusing! How the core do that?
thank you.
It's handled by Session_Database class in Database module
See the source: https://github.com/kohana/database/blob/3.2/master/classes/kohana/session/database.php
To implement database session mechanism there is Auth_ORM class in Kohana 3.2.

Resources