Generating query strings in Laravel 4 - string

I have a named route
Route::get('login/facebook', array(
'as' > 'login.facebook.authorise',
'uses' => 'AuthController#getFBAuthorise'
));
and want to produce a URL pointing to it with a query string appended.
For example:
URL::route('login.facebook.authorise', array("next"=>'/dashboard/')
would produce
/login/facebook?next=/dashboard/
Is something like this built in to the framework? If not, what is the best way to try and do this?
Thanks.

The URL helper can do this.
http://laravel.com/docs/helpers#urls
echo link_to_route('route.name', $title, $parameters = array(), $attributes = array());
EDIT: To get only the URL :
http://laravel.com/docs/routing#named-routes
$url = URL::route('route.name', $parameters);

I am a bit late to the party
Just dove into the classes and methods for building urls and you can do this:
link_to_route('login.facebook.authorise','Facebook Login',array('next'=>'/Dashboard/'))
So. the array will always fallback to query params unless the names are declared in the route.

If the use case for this is redirecting to a page after logging in then you could use
Redirect::intended('dashboard');
As seen here http://laravel.com/docs/security#authenticating-users
Sorry if that's not what you're trying to achieve.

Related

How to get current product or category url equivalent for all other channels in shopware 6?

I need to build a "channel switcher" in shopware6. How can I access current (Product)URL equivalents for all or some other channels in this shop? It should work like a language switcher, keeping the current viewed product or category and redirecting to other channel.
I know how to get current seoUrl of product:
{{ seoUrl('frontend.detail.page', { productId: page.product.id }) }}
but is there a way to get all other seoUrls for all other selling channels with the same product.id? And the same for categories?
Rough approach to get all language links
Looking at
\Shopware\Core\Framework\Adapter\Twig\Extension\SeoUrlFunctionExtension which is handling the seoUrl() in twig it looks like this:
public function seoUrl(string $name, array $parameters = []): string
{
return $this->seoUrlReplacer->generate($name, $parameters);
}
This resolves to
\Shopware\Core\Content\Seo\SeoUrlPlaceholderHandler::generate which always uses the current request:
public function generate($name, array $parameters = []): string
{
$path = $this->router->generate($name, $parameters, RouterInterface::ABSOLUTE_PATH);
$request = $this->requestStack->getMainRequest();
$basePath = $request ? $request->getBasePath() : '';
$path = $this->removePrefix($path, $basePath);
return self::DOMAIN_PLACEHOLDER . $path . '#';
}
Which boils down to \Shopware\Storefront\Framework\Routing\Router::generate
As an approach I would suggest to dig into those functions. They directly access the current request and context. Thanks to dependency injection, you can create those instances yourself with the target context and an artificial request containing the right base URL from the target channel.
Approach similar to the language switcher (redirect after POST)
The above approach might be quite complicated, so looking at what the language switcher does:
It just includes the language IDs
On selection posts to \Shopware\Storefront\Controller\ContextController::switchLanguage
Which does the redirect
Example Payload of the request:
languageId: 2fbb5fe2e29a4d70aa5854ce7ce3ff0b
redirectTo: frontend.navigation.page
redirectParameters[navigationId]: a11f6b46948c47a7b2c2ac874704fff6
I think you can extend that script for your channel switcher and add the sales channel id to the request,
then you can reuse / copy or even decorate the switchLanguage controller. You just need to pass in the right context of the target sales channel and it should redirect to the correct URL.

append a value to existing url in reactjs

I have the following url and a parameter :
number:200
"http://localhost:8000/textpath"
I want the path to be like this:
"http://localhost:8000/textpath/200"
How do I do that using Reactjs?I want to use the appended url in fetch method as follows:
fetch("http://localhost:8000/textpath/200")
A simple append did the work.Doesn't have to complicate
fetch("http://localhost:8000/textpath/"+number)
Try using Template literals.
const url = `http://localhost:8000/textpath/${number}`
fetch(url);
where number = 200
Refer here for more information

woocommerce get attribute terms

In Woocommerce you can add global product attributes and terms. So for instance:
Size (attribute)
small (term)
medium (term)
large (term)
This is product independent. You can then select from the pre defined attributes on a product.
I need to get all of the terms in an attribute with php. So select the attribute required, eg size, and then return an array including [small,medium,large].
Seems simple enough but I can't find any help on doing this.
Slightly confusing, especially when looking through the WooCommerce Docs since there is absolutely no mention of getting a list of the terms/attributes.
The Attributes are saved as a custom taxonomy, and the terms are taxonomy terms. That means you can use native Wordpress functions: Wordpress get_terms() Function Reference
By clicking on an attribute in WooCommerce, you can look in the URL and you can see they are all prepended with 'pa_'
This is likely what you need:
$terms = get_terms("pa_size");
foreach ( $terms as $term ) {
echo "<option>" . $term->name . "</option>";
}
I wanted to be able to get all the different attributes from the backend that were set, and get them in an array for me to work with, I took some code from the class-wc-admin-attributes.php file and modified it for my needs:
<?php
$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();
if ($attribute_taxonomies) :
foreach ($attribute_taxonomies as $tax) :
if (taxonomy_exists(wc_attribute_taxonomy_name($tax->attribute_name))) :
$taxonomy_terms[$tax->attribute_name] = get_terms(wc_attribute_taxonomy_name($tax->attribute_name), 'orderby=name&hide_empty=0');
endif;
endforeach;
endif;
var_dump($taxonomy_terms);
exit;
This will loop through all the attribute taxonomies, retrieve the terms for each, leaving you with an array of term objects to work with for each taxonomy.
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array:
$terms = get_terms( array(
'taxonomy' => 'pa_taxonyname',
'hide_empty' => false,
) );
For example, if the taxonomy slug is 'date', so the taxonomy will be 'pa_date'.
You can also mouse over the attribute name and see the taxonomy name at the bottom of the browser.
I hope it helps!
I use this:
echo '<h1>variations</h1>';
mario( $product->get_available_variations());
echo '<h1>Atributos</h1>';
mario($product->get_attributes());
echo '<h1>Poste Terms</h1>';
mario(wp_get_post_terms( $post->ID, 'pa_color'));
function mario($texto){
echo '<pre>';var_dump($texto);echo '</pre>';
};
Really with: "wp_get_post_terms( $post->ID, 'pa_color')" i search only one term, but the idea is to loop for the key ['name'] that return that function.
Get all attributes with attribute terms in woocommerce.
$attributes = wc_get_attribute_taxonomies();
foreach ($attributes as $attribute) {
$attribute->attribute_terms = get_terms(array(
'taxonomy' => 'pa_'.$attribute->attribute_name,
'hide_empty' => false,
));
}

PHP. Write an anchor in the Smarty template. (Kohana 3 + KSmarty)

I'm learning Kohana 3.2.0 together with KSmarty for Kohana 3. I'd like to write an anchor on the page like this:
Page list
I can build the url in the controller and pass it to Smarty as a variable but. Is there a way to build the anchor or URL in Smarty template (including "http://www.mysite.cz" part)?
If it is not possible to build the anchor. Is it at least possible to build full URL?
The Reason: I have a main template which includes another template.
The main template will be used by multiple controllers and I would like to avoid building the URL in each controller. Therefore I'll be happy if KSmarty will be able to do it for me.
The only solution I have found is to write custom function. Save following code into function.url.php file in Smarty plugins directory:
function smarty_function_url($params, &$smarty)
{
$type = '';
if(isset($params['type'])) $type = $params['type'];
$protocol = 'http';
if(isset($params['protocol'])) $protocol = $params['protocol'];
$url = '';
if(isset($params['url'])) $url = $params['url'];
$text = '';
if(isset($params['text'])) $text = $params['text'];
switch($params['type'])
{
case 'url':
return Kohana_URL::site($url, $protocol);
case 'anchor':
$url = Kohana_URL::site($url, $protocol);
return "<a href='{$url}'>{$text}</a>";
default:
return Kohana_URL::base('http');
}
}
Examples of use in Smarty template:
{url}
{url type='url' url='admin/categories' protocol='https'}
{url type='anchor' url='admin/articles' text='List of articles'}
The first block in which variables are set I had to write otherwise Smarty was generating notice "Undefined variable...". I'm just PHP student, suggestions for code improvement are welcome.
Hope it will help the others.

Kohana/ORM - Assign relations to views

I have an object in a controller's method:
$post = ORM::factory('post', array('slug' => $slug);
that is sent to the view:
$this->template->content = View::factory('view')->bind('post', $post);
I've created 1-n relation between post and comments. So good so far.
The main issue is: how should I pass post comments to the view? Currently I'm getting them in the view ($post->comments->find_all()) but I don't feel it's the best method (and not matching MVC standards in my humble opinion). I was also thinking about assigning them to the property in the controller ($post->comments) however I get an error about undefined property (which makes sense to me).
How would you recommend to solve that issue?
Why not grab the comments in the controller and pass them to the view for presentation? At least that's how I'd go about this.
$post = ORM::factory('post', array('slug' => $slug));
$comments = $post->comments->find_all();
$this->template->content = View::factory('view')->bind('comments', $comments);
In regards to your multiple pages comment, which I assume you mean posts...
This is what I would usually do.
$posts = ORM::factory('post', array('slug' => $slug))->find_all();
$view = new View('view');
foreach ($posts as $post) {
$view->comments = $post->comments;
$this->template->content .= $view->render();
}
Though this may not be the most resource friendly way to accomplish this, especially if you're working with many posts. So passing the posts to the view and then doing the foreach loop inside the view may be a better way to go about this.
$posts = ORM::factory('post', array('slug' => $slug))->find_all();
$this->template->content = View::factory('view')->bind('posts', $posts);
Though I also don't necessarily think running a select query from the view is the worst thing in the world. Though I'm no expert... ;)
I had asked this question a while ago in regards to CodeIgniter... passing an Array to the view and looping through it seemed to be the favored response...
Using CodeIgniter is it bad practice to load a view in a loop

Resources