Identify the generated URL path - laravel

I've this github package for my Laravel application, using composer, which creates sitemaps, but I am not able to understand where those sitemaps are rendered.
I had luck in finding the main sitemap after hitting /sitemap.xml URL location of my website, but I am unable to find other sitemaps like Google News sitemap one, which is supported by the package.
The responsible function seems this one, but my coding skills restrict me from understanding the rendered path:
/**
* Returns document with all sitemap items from $items array
*
* #param string $format (options: xml, html, txt, ror-rss, ror-rdf, google-news)
* #param string $style (path to custom xls style like '/styles/xsl/xml-sitemap.xsl')
*
* #return View
*/
public function render($format = 'xml', $style = null)
{
// limit size of sitemap
if ($this->model->getMaxSize() > 0 && count($this->model->getItems()) > $this->model->getMaxSize())
{
$this->model->limitSize($this->model->getMaxSize());
}
else if ('google-news' == $format && count($this->model->getItems()) > 1000)
{
$this->model->limitSize(1000);
}
else if ('google-news' != $format && count($this->model->getItems()) > 50000)
{
$this->model->limitSize();
}
$data = $this->generate($format, $style);
if ('html' == $format)
{
return $data['content'];
}
return $this->response->make($data['content'], 200, $data['headers']);
}
Im generating my sitemap on HomeController, with this function:
public function sitemap()
{
$settings_general = Utils::getSettings("general");
if ($settings_general->generate_sitemap == 1) {
// create new sitemap object
$sitemap = App::make("sitemap");
// get all posts from db
$posts = DB::table('posts')->orderBy('created_at', 'desc')->limit(600)->get();
// add every post to the sitemap
foreach ($posts as $post) {
$sitemap->add(URL::to('/') . "/" . $post->slug, $post->updated_at, '1', 'hourly', null, $post->title);
}
$pages = DB::table('pages')->orderBy('created_at', 'desc')->get();
// add every page to the sitemap
foreach ($pages as $page) {
$sitemap->add(URL::to('/') . "/" . $page->slug, $page->updated_at, '1', 'hourly', null, $page->title);
}
$categories = DB::table('categories')->orderBy('created_at', 'desc')->get();
// add every category to the sitemap
foreach ($categories as $category) {
$sub_categories = SubCategories::where('parent_id', $category->id)->get();
$sitemap->add(URL::to('/') . "/category/" . $category->slug, $category->updated_at, '1', 'hourly', null, $category->title);
foreach ($sub_categories as $sub_category) {
$sitemap->add(URL::to('/') . "/category/" . $category->slug . "/" . $sub_category->slug, $category->updated_at, '1', 'hourly', null, $category->title);
}
}
return $sitemap->render('xml');
}
}

Related

ErrorException in AssetController.php line 21: Trying to get property of non-object

I have this code source that i'm trying to use in one of my projects, it worked with laravel 5.2. This the function in the assetController:
namespace App\Http\Controllers;
use App\Setting;
use File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AssetController extends Controller
{
/**
* List all image from image directory
*/
public function getAsset()
{
//Get Admin Images
$adminImages = array();
//get image file array
$images_dir = Setting::where('name', 'images_dir')->first();
$folderContentAdmin = File::files($images_dir->value);
//check the allowed file extension and make the allowed file array
$allowedExt = Setting::where('name', 'images_allowedExtensions')->first();
$temp = explode('|', $allowedExt->value);
foreach ($folderContentAdmin as $key => $item)
{
if( ! is_array($item))
{
//check the file extension
$ext = pathinfo($item, PATHINFO_EXTENSION);
//prep allowed extensions array
if (in_array($ext, $temp))
{
array_push($adminImages, $item);
}
}
}
//Get User Images
$userImages = array();
$userID = Auth::user()->id;
$images_uploadDir = Setting::where('name', 'images_uploadDir')->first();
if (is_dir( $images_uploadDir->value . "/" .$userID ))
{
$folderContentUser = File::files($images_uploadDir->value . "/" .$userID );
if ($folderContentUser)
{
foreach ($folderContentUser as $key => $item)
{
if ( ! is_array($item))
{
//check the file extension
$ext = pathinfo($item, PATHINFO_EXTENSION);
//prep allowed extensions array
//$temp = explode("|", $this->config->item('images_allowedExtensions'));
if (in_array($ext, $temp))
{
array_push($userImages, $item);
}
}
}
}
}
//var_dump($folderContent);
//var_dump($adminImages);
return view('assets/images', compact('adminImages', 'userImages'));
}
The problem is in the line 21 :
//get image file array
$images_dir = Setting::where('name', 'images_dir')->first();
$folderContentAdmin = File::files($images_dir->value);
From my research I find out that the reason is because the setting table is empty which it is true.
Please tell me if there is another cause to that problem if it's not the case I need a solution because I don't have a way to fill that table except doing it from the database itself (phpmyAdmin)

Magento: Canonical Link disappeared

I have modified the _prepareLayout() function in Mage_Catalog_Block_Category_View class to have a customized canonical url. After modifying the URL, the canonical code does not display in the html source code anymore.
Here are my codes:
protected function _prepareLayout() {
Mage_Core_Block_Template::_prepareLayout();
$this->getLayout()->createBlock('catalog/breadcrumbs');
if ($headBlock = $this->getLayout()->getBlock('head')) {
$category = $this->getCurrentCategory();
if ($title = $category->getMetaTitle()) {
$headBlock->setTitle($title);
}
if ($description = $category->getMetaDescription()) {
$headBlock->setDescription($description);
}
if ($keywords = $category->getMetaKeywords()) {
$headBlock->setKeywords($keywords);
}
if ($this->helper('catalog/category')->canUseCanonicalTag()) {
//$headBlock->addLinkRel('canonical', $category->getUrl());
if ($category->getCategoryUrlAlias()) {
$url = Mage::getBaseUrl() . $category->getCategoryUrlAlias();
} else {
$key = $this->helper('my_package/category')->getIsTitleCategoryKey($category);
$url = $category->getUrl();
$url = $this->_removeKeyFromUrl($url, $key);
}
$headBlock->addLinkRel('canonical', $url);
}
/*
want to show rss feed in the url
*/
if ($this->IsRssCatalogEnable() && $this->IsTopCategory()) {
$title = $this->helper('rss')->__('%s RSS Feed', $this->getCurrentCategory()->getName());
$headBlock->addItem('rss', $this->getRssLink(), 'title="' . $title . '"');
}
}
return $this;
}
I have verified that the $url variable always have the customized url value that I want. I'm wondering if there is any validation function that's preventing me to have a customized canonical URL?
Any help will be greatly appreciated. Thanks!

joomla tag form loads whole list from db

I noticed that joomla tag input field is quite stupid. It loads everything from db, in this case 9K tags. Obviously ui becomes so slow.
Any ideas how to fix it? it seems there is already an ajax functionality present, so why not rely on that completely? J ways are crazy.
1 idea is to modify getOption method, and load only the tags that are related to current article editor is editing.
But in this context I don't seem to have article id.
Any ideas how to solve situation? I'm sure some of you've run into this :S
/**
* Method to get a list of tags
*
* #return array The field option objects.
*
* #since 3.1
*/
protected function getOptions()
{
$published = $this->element['published']? $this->element['published'] : array(0,1);
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('DISTINCT a.id AS value, a.path, a.title AS text, a.level, a.published, a.lft')
->from('#__tags AS a')
->join('LEFT', $db->qn('#__tags') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
// Filter language
if (!empty($this->element['language']))
{
$query->where('a.language = ' . $db->q($this->element['language']));
}
$query->where($db->qn('a.lft') . ' > 0');
// Filter on the published state
if (is_numeric($published))
{
$query->where('a.published = ' . (int) $published);
}
elseif (is_array($published))
{
JArrayHelper::toInteger($published);
$query->where('a.published IN (' . implode(',', $published) . ')');
}
$query->order('a.lft ASC');
// Get the options.
$db->setQuery($query);
try
{
$options = $db->loadObjectList();
}
catch (RuntimeException $e)
{
return false;
}
// Block the possibility to set a tag as it own parent
if ($this->form->getName() == 'com_tags.tag')
{
$id = (int) $this->form->getValue('id', 0);
foreach ($options as $option)
{
if ($option->value == $id)
{
$option->disable = true;
}
}
}
// Merge any additional options in the XML definition.
$options = array_merge(parent::getOptions(), $options);
// Prepare nested data
if ($this->isNested())
{
$this->prepareOptionsNested($options);
}
else
{
$options = JHelperTags::convertPathsToNames($options);
}
return $options;
}
So I've modified list that gets preloaded, only to load tags that are present in the article (saved as belonging to article). Autocomplete still works with ajax so no loss of functionality there.

Magento sets null value for non-selected attribute value in admin. It affects the frontend display. How to deal with it?

I've created a new attribute (type: dropdown) that is not a required field.
At this moment, every product shows in the frontend "my attribute: n/a".
After save anything in some product, magento write a null value inside catalog_product_entity_int table for this attribute.
But in the frontend the attribute now appear as "my attribute: No" instead of "N/A".
It looks like a bug, since I didn't touch in the attribute while editing the new product.
Is there a way to deal with it or to apply some rule in my phtml?
Actually this is not a bug. It's a feature.
N/A is displayed when there is no record in the table catalog_product_entity_int for that attribute.
When you add an attribute there are no values for that attribute for any product, but as soon as you save a product that has that attribute, a null value is inserted in the table (as you stated). So no value is different from null value.
All the magic happens here Mage_Catalog_Block_Product_View_Attributes::getAdditionalData().
These are the lines that interest you:
if (!$product->hasData($attribute->getAttributeCode())) { // no value in the database
$value = Mage::helper('catalog')->__('N/A');
} elseif ((string)$value == '') { // empty value in the database
$value = Mage::helper('catalog')->__('No');
}
If you want to change anything override this method.
If you change anything you might want to take a look at Mage_Catalog_Block_Product_Compare_List::getProductAttributeValue().
The same system is used for displaying attribute values in the compare products list.
I've ended up to create 2 observers... One that overrides getValue from Mage_Eav_Model_Entity_Attribute_Frontend_Default and other to override getAdditionalData in Mage_Catalog_Block_Product_View_Attributes as follows:
<?php
class Namespace_Module_Model_Entity_Attribute_Frontend_Default extends Mage_Eav_Model_Entity_Attribute_Frontend_Default{
public function getValue(Varien_Object $object)
{
$value = $object->getData($this->getAttribute()->getAttributeCode());
if (in_array($this->getConfigField('input'), array('select','boolean'))) {
$valueOption = $this->getOption($value);
if (!$valueOption) {
$opt = Mage::getModel('eav/entity_attribute_source_boolean');
$options = $opt->getAllOptions();
if ($options && !is_null($value)) { //added !is_null
foreach ($options as $option) {
if ($option['value'] == $value ) {
$valueOption = $option['label'];
}
}
}
}
$value = $valueOption;
} elseif ($this->getConfigField('input') == 'multiselect') {
$value = $this->getOption($value);
if (is_array($value)) {
$value = implode(', ', $value);
}
}
return $value;
}
}
and
<?php
class Namespace_Module_Block_Product_View_Attributes extends Mage_Catalog_Block_Product_View_Attributes
{
public function getAdditionalData(array $excludeAttr = array())
{
$data = array();
$product = $this->getProduct();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
$value = $attribute->getFrontend()->getValue($product);
if (!$product->hasData($attribute->getAttributeCode()) || (string)$value == '') { //modified
$value = Mage::helper('catalog')->__('N/A');
} elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
$value = Mage::app()->getStore()->convertPrice($value, true);
}
if (is_string($value) && strlen($value)) {
$data[$attribute->getAttributeCode()] = array(
'label' => $attribute->getStoreLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode()
);
}
}
}
return $data;
}
}

Magento language switcher: category names not being translated in url

I have a store with 2 store views for two languages, italian and english.
For some categories i have different names for italian and english, like Apparel for EN and Abbigliamento for IT.
The problem is that when i am in mystore.com/it/abbigliamento if i switch language to english the language switcher brings me to mystore.com/en/abbigliamento instead of mystore.com/en/apparel, and gives me a 404 error.
the language switcher changes the store id but don't translate the category name
thanks, Pietro.
You could use a rewrite for Mage_Core_Model_Store as follows
class Example_StoreUrls_Model_Core_Store extends Mage_Core_Model_Store {
/**
* Looks up a given request path in the current store (app) and translates it to the
* value in $this store using the rewrite index
*
* You might want to throw exceptions in case of just returning the input URLs during errors.
*
* #param $requestPath
*/
public function lookupLocalizedPath($requestPath) {
$urlRewriteCollectionSource = Mage::getModel('core/url_rewrite')->getCollection();
$urlRewriteCollectionSource
->addFieldToFilter('request_path', $requestPath)
->addStoreFilter(Mage::app()->getStore());
if(count($urlRewriteCollectionSource) == 0) {
return $requestPath;
}
$idPath = $urlRewriteCollectionSource->getFirstItem()->getIdPath();
$urlRewriteCollectionTarget = Mage::getModel('core/url_rewrite')->getCollection();
$urlRewriteCollectionTarget
->addFieldToFilter('id_path', $idPath)
->addStoreFilter($this);
if(count($urlRewriteCollectionTarget) == 0) {
return $requestPath;
}
return $urlRewriteCollectionTarget->getFirstItem()->getRequestPath();
}
/**
* Copied from parent + change:
* Watch out for the inserted line
* #param bool $fromStore
* #return string
*/
public function getCurrentUrl($fromStore = true)
{
$sidQueryParam = $this->_getSession()->getSessionIdQueryParam();
$requestString = Mage::getSingleton('core/url')->escape(
ltrim(Mage::app()->getRequest()->getRequestString(), '/'));
$storeUrl = Mage::app()->getStore()->isCurrentlySecure()
? $this->getUrl('', array('_secure' => true))
: $this->getUrl('');
$storeParsedUrl = parse_url($storeUrl);
$storeParsedQuery = array();
if (isset($storeParsedUrl['query'])) {
parse_str($storeParsedUrl['query'], $storeParsedQuery);
}
$currQuery = Mage::app()->getRequest()->getQuery();
if (isset($currQuery[$sidQueryParam]) && !empty($currQuery[$sidQueryParam])
&& $this->_getSession()->getSessionIdForHost($storeUrl) != $currQuery[$sidQueryParam]
) {
unset($currQuery[$sidQueryParam]);
}
foreach ($currQuery as $k => $v) {
$storeParsedQuery[$k] = $v;
}
// inserted the following line - rest is from core
$requestString = $this->lookupLocalizedPath($requestString);
if (!Mage::getStoreConfigFlag(Mage_Core_Model_Store::XML_PATH_STORE_IN_URL, $this->getCode())) {
$storeParsedQuery['___store'] = $this->getCode();
}
if ($fromStore !== false) {
$storeParsedQuery['___from_store'] = $fromStore === true ? Mage::app()->getStore()->getCode() : $fromStore;
}
return $storeParsedUrl['scheme'] . '://' . $storeParsedUrl['host']
. (isset($storeParsedUrl['port']) ? ':' . $storeParsedUrl['port'] : '')
. $storeParsedUrl['path'] . $requestString
. ($storeParsedQuery ? '?'.http_build_query($storeParsedQuery, '', '&') : '');
}
}
In magento admin in
Catalog->Manage categories
Select category and choose preffered store view. There you should edit and save "URL key" parameter.
In case it still shows old url - clean cache and make url rewrite reindex.

Resources