I create small site with Yii2 basic, and I have problem, I search every times and not resolved this, I try rewrite with .htaccess file but not success.
My site have more category get from database example: house-building, car, job, computer, travel.....
I would like user click on category example: http://example.com/house-building => it access category/index?category=house-building
Current, my config/web.php as bellow.
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
#'suffix'=>'.html',
'rules' => array(
''=>'site/index',
'/login.html'=>'/users/login',
'/register.html'=>'/users/create',
### End custom url
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
],
Can you help me resolved this issues.
Thanks you.
Use this rule:
'<category:\w+>' => 'category/index'
Related
I am using the Yii2 Menu widget. This is my menu
echo Menu::widget([
'items' => [
'label' => 'Products',
'url' => '/products' // Not working.
'items' => [
[
'label' => 'New Products',
'url' => '/new-products'
]
]
]
]);
But the URL /products doesn't work when the user click Products, then the menu must be open (it works) and also open the URL /products (it doesn't work).
When specifying the URLs for the menu items you have to specify them
in the form of controller/action not just as /controller even if
the default action is used.
This is written on the top as a Note in the class reference yii-widgets-menu in the very first example code under comments.
So change your url for the Products item to
'url' => '/products/index'
and also for the new-products
'url' => '/products/new-products'
Hope it helps.
Note: when you specify URLs like /controller/action it will add it to the baseUrl or domain name removing everything after that.
Like if you have a domain http://example.com and you are on the page http://example.com/contact-us.
It will replace everything after the domain name and add your specified http://example.com/controller/action and if the menu item URL is provided like controller/action then clicking it will append it to the existing URL http://example.com/contact-us/controller/action.
You can't really expect that clicking "product" item will open submenu and redirect to /products page at the same. It does not make any sense, since you will be redirected to different page before you will have a chance to click something in submenu. That's why clicking on "products" only shows submenu dropdown. But this link actually works - try use middle mouse button or disable javascript in your browser.
In your case you should probably define your menu as:
echo Menu::widget([
'items' => [
'label' => 'Products',
'url' => '/products'
'items' => [
[
'label' => 'All Products',
'url' => '/products'
],
[
'label' => 'New Products',
'url' => '/new-products'
],
]
]
]);
All navigation links should be in submenu.
I have created an archive page for my 'Event' custom post ape called archive-event.php.
I used the content from the standard archive.php template and it listed my 4 events in the order they were posted.
The next step was to change the order by the custom field event_date. I did this no problems. Then I wanted to not show any events that event_date had passed. The below code does this perfectly.
The issue I now have is my pagination is all messed up. I have the default reading settings set to 2 posts per page and I have a 'Load more' button to bring up the next two.
When I click 'Load more' it duplicates the two events already showing. Any ideas where I've gone wrong?
<?php
$today = date("Ymd");
$args = array (
'post_type' => 'event',
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => $today,
'type' => 'numeric',
'compare' => '>=',
)
),
);
$wp_query = new WP_Query( $args );
while( $wp_query->have_posts() )
{
$wp_query->the_post();
?>
<div>Content goes here</div>
<?php } wp_reset_postdata(); ?>
Depending on how the plugin builds the UI it may or may not be respecting the pagination functionality built into WordPress.
Try forcing the paged setting into your query like so:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array (
'post_type' => 'event',
'paged' => $paged, // this will be set to what WordPress thinks is the proper page to start
'meta_key' => 'event_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => $today,
'type' => 'numeric',
'compare' => '>=',
)
),
);
Next, if this still doesn't work, you can test the assumption that the paged parameter isn't working for some reason by changing the default paged value like so (again, just to test):
$paged = 2; // just force it to start on the second page to see if that works
If the above test works, I would say that get_query_var() believes you're on the first page.
I need to access the current user logged in (if any) in any part of the application. Special paths like /admin requires special permissions (roles).
This is the firewall configuration (just one firewall) protecting the entire application but not allowing anonymous users (because I need the current user even in homepage).
I got a redirect loop even requesting /. Any help?
'security.firewalls' => array(
'secured' => array(
'pattern' => '.*',
'anonymous' => false,
'form' => array(
'login_path' => '/login',
'check_path' => '/login_check',
'username_parameter' => 'login[username]',
'password_parameter' => 'login[password]',
),
'logout' => array('logout_path' => '/logout')
)
)
Access rules requires ROLE_ADMIN only for paths starting with /admin . The rest is anonymous:
'security.access_rules' => array(
array('^/admin', 'ROLE_ADMIN'),
array('^.*', 'IS_AUTHENTICATED_ANONYMOUSLY')
),
To allow acces via IS_AUTHENTICATED_ANONYMOUSLY have have to allow anonymous.
'security.firewalls' => array(
'secured' => array(
'pattern' => '/',
'anonymous' => true,
// other stuff
)
)
'security.access_rules' => array(
array('^/admin', 'ROLE_ADMIN'),
array('^/', 'IS_AUTHENTICATED_ANONYMOUSLY')
),
If the user is logged in you can access them in every page. IS_AUTHENTICATED_ANONYMOUSLY is only a role, which have unauthenticated users (anonymous).
I use default kohana 3.2 pagination module.
The search returns about 100 pages of results.
And pagination module renders all 100 links.
How to fix this?
Here is your default pagination config:
return array(
// Application defaults
'default' => array(
'current_page' => array('source' => 'query_string', 'key' => 'page'), // source: "query_string" or "route"
'total_items' => 0,
'items_per_page' => 10,
'view' => 'pagination/basic',
'auto_hide' => TRUE,
'first_page_in_url' => FALSE,
),
);
All you need is switch pagination view from pagination/basic to pagination/floating. Or you can create and use your own pagination template.
While I was learning kohana and making single article, i encountered this problem.
I wanted to get my url to look like article/post_id/slug(optional) so I made the route.
Route::set('article', '/<controller>/<article_id>(/<article_slug>(/<id1>(/<id2>(/<id3>))))', array('article_id' => '[0-9]', 'article_slug' => '[a-zA-Z0-9_]+'))
->defaults(array(
'controller' => 'article',
'action' => 'read',
));
I even tried without 3rd parameter for Route::set but anyway returns error shown in the picture.
Thanks. (:
Try without the leading /:
Route::set('article', '<controller>/<article_id>(/<article_slug>(/<id1>(/<id2>(/<id3>))))',
array(
'article_id' => '[0-9]',
'article_slug' => '[a-zA-Z0-9_]+')
)->defaults(
array(
'controller' => 'article',
'action' => 'read',
));