Redirect
Retrieve a list of all redirects
All retrieval examples utilise the Yaspa\AdminApi\Redirect\RedirectService::getRedirects
method, which
returns a Yaspa\Builders\PagedResultsIterator
that will provide an iterate-able list of
Yaspa\AdminApi\Redirect\Models\Redirect
models.
The getRedirects
method accepts a Yaspa\AdminApi\Redirect\Builders\GetRedirectsRequest
instance,
which is a request builder that will provide hints to what options are available assuming one is using
an IDE such as PHPStorm.
The following Shopify note should also be considered:
A redirect's "path" attribute is the path which activates this redirect when visited, and a shop cannot have more than one redirect with the same path. The "target" attribute is the URL which the visitor is redirected to when they try to access the associated path. The target could either be a path or a full URL, possibly even for a different domain.
Get a list of all URL redirects for your shop after a specified ID
The example demonstrates how to get all redirects after a provided id.
The response is a Yaspa\Builders\PagedResultsIterator
that will provide an iterate-able list of
Yaspa\AdminApi\Redirect\Models\Redirect
models.
use Yaspa\AdminApi\Redirect\Builders\GetRedirectsRequest;
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Create request parameters
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetRedirectsRequest::class)
->withCredentials($credentials)
->withSinceId(668809255);
// Get redirects
$service = Factory::make(RedirectService::class);
$redirects = $service->getRedirects($request);
Get a list of all URL redirects
The example demonstrates how to get all redirects for a store.
The response is a Yaspa\Builders\PagedResultsIterator
that will provide an iterate-able list of
Yaspa\AdminApi\Redirect\Models\Redirect
models.
use Yaspa\AdminApi\Redirect\Builders\GetRedirectsRequest;
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Create request parameters
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(GetRedirectsRequest::class)
->withCredentials($credentials);
// Get redirects
$service = Factory::make(RedirectService::class);
$redirects = $service->getRedirects($request);
Get a count of all URL redirects for your shop
The example demonstrates how to get a count of redirects.
As the count endpoint accepts parameters, the request is a Yaspa\AdminApi\Redirect\Builders\CountAllRedirectsRequest
object. The object provides methods for setting available parameters such as withTarget
.
The response is an integer representing the redirect count that matches the parameters provided.
use Yaspa\AdminApi\Redirect\Builders\CountAllRedirectsRequest;
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Create request parameters
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$request = Factory::make(CountAllRedirectsRequest::class)
->withCredentials($credentials);
// Get count
$service = Factory::make(RedirectService::class);
$result = $service->countRedirects($request);
Get a single redirect
The example demonstrates how to get a single redirect by its ID.
The response is a Yaspa\AdminApi\Redirect\Models\Redirect
model.
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Create request parameters
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Get redirect
$service = Factory::make(RedirectService::class);
$retrievedRedirect = $service->getRedirectById($credentials, 668809255);
Create a new redirect
Creating a new redirect involves creating and populating a Yaspa\AdminApi\Redirect\Models\Redirect
instance.
The method Yaspa\AdminApi\Redirect\RedirectService::createNewRedirect
will generally return a redirect model
back. Although it seems redundant, one should still use the Shopify returned redirect model as the trusted version of
the created redirect given that Shopify is the source of truth.
Create a new relative redirect
Shopify describes the example as:
We expect users might try going to /ipod in order to find about one of our popular products, but we want to redirect them to /itunes because that's where we have the information they're looking for.
The response is a Yaspa\AdminApi\Redirect\Models\Redirect
model.
use Yaspa\AdminApi\Redirect\Models\Redirect;
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Create request parameters
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$redirect = (new Redirect())
->setPath(uniqid('/ipod-'))
->setTarget('/pages/itunes');
// Create new redirect
$service = Factory::make(RedirectService::class);
$newRedirect = $service->createNewRedirect($credentials, $redirect);
Create a new full URL redirect
Shopify describes the example as:
We have a separate forums site that is on a different subdomain. The "path" is always converted to an absolute path without a domain, but the "target" can be a full URL.
The response is a Yaspa\AdminApi\Redirect\Models\Redirect
model.
use Yaspa\AdminApi\Redirect\Models\Redirect;
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Create request parameters
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$redirect = (new Redirect())
->setPath(uniqid('http://www.apple.com/forums/'))
->setTarget('http://forums.apple.com');
// Create new redirect
$service = Factory::make(RedirectService::class);
$newRedirect = $service->createNewRedirect($credentials, $redirect);
Trying to create a redirect without a path and target will return an error
The request attempts to create a redirect with no details.
The result is a Guzzle exception. Although it may vary depending on how the Guzzle client is configured. See Guzzle http_errors options for more information.
use Yaspa\AdminApi\Redirect\Models\Redirect;
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Create request parameters
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$redirect = new Redirect();
/**
* Create redirect, a Guzzle exception will be thrown
*/
$service = Factory::make(RedirectService::class);
$newRedirect = $service->createNewRedirect($credentials, $redirect);
Modify an existing redirect
Modifying, or updating, a redirect involves populating a Yaspa\AdminApi\Redirect\Models\Redirect
instance,
however, the id
attribute must be set.
Please note that Yaspa does not defensively check that an id exists in a redirect model when used in an update request as the expectation is that Shopify will return an error.
Change a redirect so that it activates for a different request path
The examples shows how to modify an existing redirect so that it activates for a different path.
The response is a Yaspa\AdminApi\Redirect\Models\Redirect
model.
use Yaspa\AdminApi\Redirect\Models\Redirect;
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Create request parameters
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$redirectUpdate = (new Redirect())
->setId(668809255)
->setPath('/tiger');
$request = Factory::make(ModifyExistingRedirectRequest::class)
->withCredentials($credentials)
->withRedirect($redirectUpdate);
// Get and Test results
$service = Factory::make(RedirectService::class);
$updatedRedirect = $service->modifyExistingRedirect($request);
Change both the path and target URIs for an existing redirect
The examples shows how to modify an existing redirect so that it is completely different.
The response is a Yaspa\AdminApi\Redirect\Models\Redirect
model.
use Yaspa\AdminApi\Redirect\Models\Redirect;
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Create request parameters
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
$redirectUpdate = (new Redirect())
->setId(950115854)
->setPath('/powermac')
->setTarget('/pages/macpro');
$request = Factory::make(ModifyExistingRedirectRequest::class)
->withCredentials($credentials)
->withRedirect($redirectUpdate);
// Update redirect
$service = Factory::make(RedirectService::class);
$updatedRedirect = $service->modifyExistingRedirect($request);
Remove a Redirect from the database
The example shows how to delete a redirect using its id.
The response is an empty object.
use Yaspa\AdminApi\Redirect\RedirectService;
use Yaspa\Authentication\Factory\ApiCredentials;
use Yaspa\Factory;
// Get credentials
$credentials = Factory::make(ApiCredentials::class)
->makeOAuth('johns-apparel', 'a190000000000000000000000000046a');
// Delete redirect
$service = Factory::make(RedirectService::class);
$result = $service->deleteRedirectById($credentials, $redirect->getId());