dynamic route in codeigniter don't work - codeigniter

i have problem in codeigniter routes.
i want change url from:http://example.com/test/news_details?id=17 to http://example.com/test/funy-url-maker or http://example.com/funy-url-maker
i can make $route dynamicly but it don't work.
my code:
$route[rawurlencode("'" . str_replace(' ', '-', $string)) . "'"] = "test/news_details?id=" . $id;
i print route and all route's will be add perfectly, but when i want to open http://example.com/test/funy-url-maker i will be redirect 404 page :(
any idea?
note:
when i use this as static it work perfectly.like:
$route[rawurlencode('funyNews')] = "test/b_news";

ok after all the questions and answers, I think youre looking for something like this:
foreach ($result as $row) {
$string = rawurlencode(str_replace(' ', '-', strtolower($row->subject)));
$route[$string] = "test/news_details/$row->id";
}
and your controller:
function news_details($id){
var_dump($id);
}

Related

How add multiple actions In URL?

How can I add more than one action to a URL? As I described in the title, I want to add more than one action to a URL. How do I do that?
As a further clarification, I define actions for some parts in an HTML file, and by setting an action, I handle the request in the php file!
Example:
example.com/?order=1&price=high
I appreciate your help.
As i ask and get help from my friend, we can use function like this to solve our problem:
function shapeSpace_add_var($url, $key, $value) {
$url = preg_replace('/(.*)(?|&)'. $key .'=[^&]+?(&)(.*)/i', '$1$2$4', $url .'&');
$url = substr($url, 0, -1);
if (strpos($url, '?') === false) {
return ($url .'?'. $key .'='. $value);
} else {
return ($url .'&'. $key .'='. $value);
}
}
example:
$url = 'http://example.com/whatever/?hello=world';
shapeSpace_add_var($url, 'goodbye', 'nightclub');
Result:
http://example.com/whatever/?hello=world&goodbye=nightclub

Magento hreflang Implementation

Using code from a previous discussion (link:https://magento.stackexchange.com/questions/12504/how-to-add-hreflang-tags-or-other-meta-tags-to-pages-in-magento), I was able to implement the hreflang links into our Magento site.
Here is the code that worked for me:
<?php foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
$storeId = $store->getId();
$storeCode = substr(Mage::getStoreConfig('general/locale/code', $storeId),0,2);
if (Mage::registry('product')) {
$productId = Mage::registry('product')->getId();
$base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId)->getProductUrl();
$url = preg_replace('/\?.*/', '', $url);
echo '<link rel="alternate" hreflang="' . $storeCode . '" href="' . $url . '"/>';}
elseif(Mage::registry('current_category')) {
$categoryId = Mage::registry('current_category')->getId();
$base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryId)->getUrlPath();
echo '<link rel="alternate" hreflang="' . $storeCode . '" href="' . $base_url . $url . '"/>' . "\n";
}}}}
?>
<?php
$storeId = 1;
if (Mage::registry('product')) {
$productId = Mage::registry('product')->getId();
$base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($productId)->getProductUrl();
$url = preg_replace('/\?.*/', '', $url);
echo '<link rel="alternate" hreflang="x-default" href="' . $url . '"/>';
}
elseif(Mage::registry('current_category')) {
$categoryId = Mage::registry('current_category')->getId();
$base_url = Mage::app()->getStore($storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryId)->getUrlPath();
echo '<link rel="alternate" hreflang="x-default" href="' . $base_url . $url . '"/>' . "\n";
}
?>
I'm having an issue with layered navigation URL's and canonical links disappearing on category pages.
Is there something I can add to this code to make sure that layered navigation URL's that contain "?" after .html get written as shown in the browsers address bar?
Also, on these types of category pages the canonical link does not show.
The code works perfectly on product pages.
Any help would be greatly appreciated!
Thanks in advance.
To solve this I need to know:
Where have you put this code? Is executed in categories?
Is the current_category empty?
Regards.
I missunderstood the question. Could you show an example?
The code only shows the category base URLs. When applying filters the module change the URL adding parameters filtered but your code doesn't have those parameters.
Usually people do not position the filters because it gives many problems of duplicate content. Take a look at this link:
https://amasty.com/blog/magento-layered-navigation-best-settings-for-seo/
In my opinion, I would left canonical URLs in categories using its base URL and disallow to index anything layered because could give duplicated content issues.
Kind regards

Dynamic Routing (Auto Create Route) on Laravel 5 - View not rendered on App::call()

Basically, I have an admin (CMS) for my app. I have included menu and submenu setup, which are eventually populated on my nav section.
Name: User
URL: setup/user
Icon: fa fa-user
The problem is, if I add new menu (like above), I have to setup another route on web.php, like:
Route::resource('setup/user','UserController');
Well, there will be lots of menu to be created and my web.php is somehow messed up with lots of routes.
I followed this article and it gave me an idea, I tweaked it and created my own.
https://laracasts.com/discuss/channels/laravel/dynamically-calling-a-controller-method-in-laravel-5
Here's what I've done:
function wildcard($route, $controller, $verbs = ['get', 'post']) {
$stack = Route::getGroupStack();
$namespace = end($stack)['namespace'];
$controller = ucfirst(strtolower($route)).'Controller';
Route::match($verbs, rtrim($route, '/') . '/{method}', function($method, $params = null) use ($route, $namespace, $controller)
{
$controller = $namespace . '\\' . $controller;
$method = $controller . '#' . $method;
$params
? App::call($method, explode('/', $params))
: App::call($method);
});
}
wildcard('home',null);
My HomeController:
public function index()
{
return view('pages.common.dashboard');
}
But it doesn't display the view on my index method, just blank page. But it did get the method because I can dump from the method.
Maybe there's a better way on dynamic routing. Thanks.
Solution: You must return your App::call()
Route::match($verbs, rtrim($route, '/') . '/{method}', function($method, $params = null) use ($route, $namespace, $controller)
{
$controller = $namespace . '\\' . $controller;
$method = $controller . '#' . $method;
if($params){
// return the App::call
return App::call($method, explode('/', $params));
} else {
// return the App::call
return App::call($method);
}
});

How to bypass or reset a filter manually?

I want to show filter of category on submenu, my code works!!
My problem is that if page are already filtered, my code does not return the options
I believe it has to do something in the code that bypasses the filter page and again bring the options in the submenu even if already have the filter on page
HTML of submenu:
{{block type="core/template" category="3" template="page/html/icons_submenu.phtml"}}
Content of page icons_submenu.phtml:
<?php
$layer = Mage::getModel("catalog/layer");
$category = Mage::getModel('catalog/category')->load($this->getCategory());
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getAttributeCode() == 'color') {
$filterBlockName = 'catalog/layer_filter_attribute';
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
echo '<strong>Color:</strong><br />';
foreach($result->getItems() as $option) {
echo ' ' . $option->getValue() . ' - ' . $option->getLabel() . '<br />';
}
}
}
?>
Example:
I would really suggest you to actually move all that logic into a proper module, a proper block and a proper model and not in a template like you are doing right now.
If you actually want further help for that, feel free to ask, making something according to the coding guide lines of Magento would make you even happier of your job, I can assure you.
This being said, what you actually want is a current filter model based on the current category and a specify attribute.
You don't need to go by the block catalog/layer_filter_attribute in a way to do this, you can directly go by the model based on the layer you already load.
So, this way of doing it should work, although it should not be in a template or view, once again :
<?php
$category = Mage::getModel('catalog/category')
->load($this->getCategory());
$layer = Mage::getModel('catalog/layer')
->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getAttributeCode() == 'color') {
// $filterBlockName = 'catalog/layer_filter_attribute';
/** This is actually your only problem in your code **/
// $result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
/** But would work with this line **/
$result = Mage::getModel('catalog/layer_filter_attribute')
->setLayer($layer)
->setAttributeModel($attribute);
echo '<strong>Color:</strong><br />';
foreach($result->getItems() as $option) {
echo ' ' . $option->getValue() . ' - ' . $option->getLabel() . '<br />';
}
}
}
?>
Then you can see it still works based on only the colours I have in the current category
But also when the category is already filtered on a specific colour

Laravel 5 link_to_* with icon/html

Anyone figured out how to put HTML inside $title parameter of link_to_* helper functions?
I've been googling around and found some topics about it directly on laravel.io but there are only suggestions:
make your own helper (tried but in the end I use link_to_* or App::make('html')->linkRoute(...); example helpers.
generate links inside views and use URL facade
Use HTML facade to encode links in views (I think this is not secure, do not know why just feels like it is wrong).
To make myself clear I am asking how to make 1. suggestion work? (I tried helpers but result is the same when using L5 helper link_to_route('some.route', 'This is title with icon <i class="icon"></i>'); and helper that I made (code below). Where $icon = '<i class="some-icon"></i>';
if ( ! function_exists('link_to_route_icon'))
{
function link_to_route_icon($name, $title = null, $icon = null, $parameters = array(), $attributes = array())
{
return app('html')->linkRoute($name, $title . $icon, $parameters, $attributes);
}
}
both (my own helper or L5 helper) output the same result:
visible in browser: name<i class="fa-sort-alpha-asc"></i>
html code: name<i class="fa-sort-alpha-asc"></i>
Solution
if ( ! function_exists('link_to_route_icon'))
{
function link_to_route_icon($name, $title = null, $icon = null, $parameters = array(), $attributes = array())
{
$url = route($name, $parameters);
return '<a href="' . $url . '"' . app('html')->attributes($attributes) . '>' . htmlentities($title) . ' ' . $icon . '</a>';
}
}
Please note when using L5 and you want to use your own helper.php, you have to load it in composer.json
"psr-4": {
"App\\": "app/"
},
"files": [
"app/Helpers.php"
]
The problem is that you are using the linkRoute method which eventually calls htmlentities on the passed title string. You have to build the HTML yourself to make it work. Here's a generic example (feel free to optimize it more for the purpose of generating icon links)
function link_to_route_html($name, $html, $parameters = array(), $attributes = array())
{
$url = route($name, $parameters);
return '<a href="'.$url.'"'.app('html')->attributes($attributes).'>'.$html.'</a>';
}
And then you call it like this:
link_to_route_html('some.route', 'This is title with icon <i class="icon"></i>');

Resources