0

I'm using Magento 2 to create my site. I want to dynamically assign a sponsor to the register of a new customer. I had create a customer custom field named nickname on the database. In practice I want to provide a link such as http://mywebsite.net/John1 where John1 is the nickname of the sponsor. The system will give an error not finding the requested page. I want to edit 404 page and redirect for the registering page with the sponsored nickname on it. I create a link:

http://mywebsite.net/John1

The 404 page will process the URL and catch the string John1 and assign to the field nickname in a way that the registering page should be such as:

Sponsor: John1
Name:__________________
E-mail: _______________
Etc.

Could someone help me on this hard task? At least some guidelines how to start would be much appreciated.

0

To achieve this requirement, you have to understand two things:

Using this two, you can achieve your requirement.

Use of custom route means to point a custom URL http://example.com/index.php/mytesturl/ to Magento own routing http://example.com/index.php/front_name/controller/action/ .

A good example of a custom router that is Magento own CMS module

Suppose you have created two cms pages from admin:

One is about us and it is identifier is about-us and its id is 5.

Then if you hit the url www.example.com/about-us then you can see the content is server from admin about-us cms page. If you debug that page www.example.com/about-us and then you will find that the page is rendered from Action class

Magento\Cms\Controller\Page\View

Internally, www.example.com/about-us is requesting the page www.example.com/cms/page/view/page_id/5.

You can check this point logic is manage fro router class Magento\Cms\Controller\Router , the businesses logic of cms page routing is managed from here.

$page = $this->_pageFactory->create();
$pageId = $page->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());
if (!$pageId) {
    return null;
}
$request->setModuleName('cms')->setControllerName('page')->setActionName('view')->setParam('page_id', $pageId);
$request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier)

So, you need to implement the same type of business routing logic for your requirement.

Where http://mywebsite.net/John1 internally request to url

http://mywebsite.net/{customFrontenname}/{Mycontrollername}/{MyActionName}/customer_id/{CustomerId}

At Custom router check request path $request->getPathInfo(), is match with a customer nickname .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.