Laravel 4 pagination links ignore existing GET parameters - laravel

I have a search in Laravel 4. After search for sh my url is this:
http://localhost/musiter/product/search-products/24?query=sh
I also have pagination there, but the pagination for the second link for example is:
http://localhost/musiter/product/search-products/24?page=2
instead of:
http://localhost/musiter/product/search-products/24?query=sh&page=2
Any way to fix this?

The code you're looking for is:
$paginator->appends(['query' => $value])->links();
You can add to the query string of pagination links using the appends method on the Paginator:
<?php echo $users->appends(array('sort' => 'votes'))->links(); ?>
This will generate URLs that look something like this:
http://example.com/something?page=2&sort=votes
If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method:
<?php echo $users->fragment('foo')->links(); ?>
This method call will generate URLs that look something like this:
http://example.com/something?page=2#foo
You can also check [Laravel documentation].1

Related

How to convert CodeIgniter urls into user friendly ones?

I've explored lot of questions and articles regarding this but I can't find how to get this done.
I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)
Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)
My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.
Thanks for reading.
Edit:
Route.php
$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;
This is how I'm building the anchor links (view_file->index.php, called from Index method):
<?php
foreach model(in the table)
echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
end foreach
?>
I can get the desired URLs with following code in route.php. Only problem is I'm not able to make the 'urlController/urlMethod' return a value in the function which can be assigned to $result variable.
$route['(:any)'] = function ($1)
{
$result = 'urlController/urlMethod/'.$1;
return $result;
};
I'm not sure how to do this. Can someone suggest how I should call 'urlController/urlMethod'?
You could achieve it with CodeIgniter URI Routing. Considering
localhost/sitename/galaxy-tab-s3
maps to
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
And, model id i.e 4, in this case, is static with respect to galaxy tab s3, as you have not mentioned any such Id in the simplified URL.
My understanding is with every URL localhost/sitename/iphone-6, you need three details about the string 'iphone-6'. i.e. type of product, model-slug, model id. One way could be write something like
$route['sitename/(:any)'] = 'routingController/commonRoutingMethod/$1';
Here, create a new routingController and write some logic into commonRoutingMethod() method, which takes the string like iphone-6 and fetches its all three details i.e. product type, model id etc. And then redirects by building the exact URL using
header('Location: http://localhost/sitename/specs/display/$productType/$modelSlug/$modelId/');
NOTE : There could be more forward ways just using regex match in routes.php, given that you create diffrentiated structure of the string, based on product type and model id e.g p_iphone-6_1 or t_galaxy-tab-s3_4.
Please use below routing code, to achieve it.
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
$route['(:any)'] = 'specs/display/$1/$1/$1';
Let me know if you need any help.

Laravel 5 append anchor links for pagination numbers

How can I append a #something to the end of the page number.
For example, this is what I want:
From:
abc.com/xxx/?page=2
To:
abc.com/xxx/?page=2#my-anchor
You can read about pagination in the docs here; Laravel Pagination.
What you are looking for is ->fragment('my-anchor').
Assuming you are using eloquent,
$links = App\YourModel::paginate(15)->fragment('my-anchor')->links();

laravel paginate passing additional var

let say i have url like this
http://localhost:8080/myweb/listproduct?idcategory=3
but when i used laravel pagination link, my passing variabel by url disappear like this
http://localhost:8080/myweb/listproduct?page=2
i want laravel paginate to pass my url variable too like this
http://localhost:8080/myweb/listproduct?idcategory=3&page2
so far i already try to set baseurl like this
$listproduct = DB::table('product')->paginate(15);
$users->setBaseUrl('listproduct?idcategory=3');
but the result is like this (? not &)
http://localhost:8080/myweb/listproduct?idcategory=3?page2
i used laravel 4.2
you can use setPath()
$users->setPath('listproduct?idcategory=3');
or if you only want to add to the query string you may use
$users->appends(['idcategory' => '3'])
You can use this method to append more arguments to your pagination link:
$listproduct->appends(['idcategory' => $id])->links();

How to call magento module in custom page?

I have added this:
echo $this->getLayout()->createBlock('core/template')->setTemplate('sales/order/history.phtml')->toHtml();
to mypage.phtml, but it is giving
Fatal error: Call to a member function getSize() on a non-object in C:\xampp\htdocs\puckerimages_cvs\app\design\frontend\default\pucker\template\sales\order\history.phtml on line 41
Can anybody tell me how to call core module controller in custom pages
I dont know exactly what you were tried to do..Controller is nothing but your url segment.
If you want get controller from Url use the following code,
Mage::app()->getRequest()->getControllerName();
Mage::app()->getRequest()->getActionName();
Mage::app()->getRequest()->getRouteName();
Mage::app()->getRequest()->getModuleName();
If you want get collection of data from your Module use the following code,
Mage::getModel('groupname/classname');
or
Mage::getSingleton('groupname/classname');
Example
$collection = Mage::getModel('module/model_name')->getCollection()
->addAttributeToSort('order', 'ASC')
->addAttributeToSort('last_name', 'ASC')
->addAttributeToSort('first_name', 'ASC')
;
try this
instead of "core/template" use "sales/order_history"
<?php echo $this->getLayout()->createBlock('sales/order_history')->setTemplate('sales/order/history.phtml')->toHtml();?>
hope this help you

Get Joomla! category from an URL

How can I read the section a certain URI belongs to?
I want to enhance the mod_breadcrumb to put section and category into the HTML. JApplication->getPathway() returns a JPathway which basically holds an assiciative array combining a name and an URL (as $list[]->name and $list[]->link). I think, it should be possible to get the section and category from a link, but don't know how.
A starting point could be the parsing into JURI-Object, but from there I don't know how get get further. Any ideas?
Pretty straight forward...
I assume you want to add category and section for the article and not your custom component.
Check if requested current URL is for article. If it is for article you know the article ID, use this article Id to go database and get catid from #__content, Use this cat_id to go to #__categories and get section (this is section id), go to #__sections to get the proper section name. All this can be done in 1 sql statement.
$breadcrumbs =& JFactory::getApplication()->getPathway();
$breadcrumbs->addItem("SECTION_NAME", JRoute::_("index.php?option=com_content&view=section&id=SECTION_ID"));
$breadcrumbs->addItem("CATEGOY_NAME", JRoute::_("index.php?option=com_content&view=category&id=CATEGORY_ID"));
$breadcrumbs->addItem("Article");
Alternatively, if you know the URL from the breadcrumb item. You can parse it and get IDS. The trick here is not to get the default URI object by JFactory::getURI() because things will get ugly, use JFactory::getURI('YOU_URI_NAME').
<?php
// You need to get Your own uri, you do not want to modify default URI
// because this will messup a lot of things
$uri = JFactory::getURI('MyCustomURI');
// Test # 1 [ID = SECTION_ID]
$url = "index.php?option=com_content&view=section&id=SECTION_ID";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
// Test # 2 [ID = 123]
$url = "index.php?option=com_content&view=section&id=123";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
?>

Resources