<p>Your code is almost there, Craft allows registering routes for the Control Panel and the front-end of the site. You need to change <code>EVENT_REGISTER_CP_URL_RULES</code> to <code>EVENT_REGISTER_SITE_URL_RULES</code>.</p>

<p>You are listening for an event on <code>EVENT_REGISTER_CP_URL_RULES</code> which will only register routes inside the control panel. This is useful for plugins that want to add controllers for settings and custom logic.</p>

<blockquote>
  <p>The event that is triggered when registering URL rules for the control panel.</p>
</blockquote>

<p><a href="https://docs.craftcms.com/api/v3/craft-web-urlmanager.html#events" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3/craft-web-urlmanager.html#events</a></p>

<p>However, in the scenario where access is needed outside of the control panel, Craft triggers the <code>EVENT_REGISTER_SITE_URL_RULES</code>. This allows access for users that do not have control panel access (e.g. site guests and regular users).</p>

<blockquote>
  <p>The event that is triggered when registering URL rules for the front-end site.</p>
</blockquote>

CC BY-SA 4.0
2020-06-16T06:06:27.150Z
Worked like a charm! Thanks!
Rohan
CC BY-SA 4.0
2020-06-15T14:58:10.277Z
2020-06-15T14:58:10.277Z
Jason McCallister
<p>I am building a custom plugin for Craft and I am hitting some REST APIs in my backend service to fetch some data using Ajax calls in JS. What I would like to do when the AJAX calls complete and come back with data, is to store some data in Craft. I know I can use projectConfig to store and retrieve plugin based data and settings but how to call those Craft functions through my JS files. The way I figured is to call it using REST routes that I can define via my plugin.</p>

<p>But I am not sure how to register routes using my plugin and how to setup controllers for the same.</p>

<p>Main plugin file:</p>

<pre><code>Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
            $event-&gt;rules['poptin/signup'] = 'poptin/default/signup';
        });
</code></pre>

<p>DefaultController.php</p>

<pre><code>&lt;?php
/**
 * poptin plugin for Craft CMS 3.x
 *
 * Create advanced website popups and forms, and improve your website's conversion rate within a few minutes.
 *
 * @link      poptin.com
 * @copyright Copyright (c) 2020 Poptin
 */

namespace poptin\poptin\controllers;

use poptin\poptin\Poptin;

use Craft;
use craft\web\Controller;

/**
 * Default Controller
 *
 * Generally speaking, controllers are the middlemen between the front end of
 * the CP/website and your plugin’s services. They contain action methods which
 * handle individual tasks.
 *
 * A common pattern used throughout Craft involves a controller action gathering
 * post data, saving it on a model, passing the model off to a service, and then
 * responding to the request appropriately depending on the service method’s response.
 *
 * Action methods begin with the prefix “action”, followed by a description of what
 * the method does (for example, actionSaveIngredient()).
 *
 * https://craftcms.com/docs/plugins/controllers
 *
 * @author    Poptin
 * @package   Poptin
 * @since     1.0.0
 */
class DefaultController extends Controller
{

    // Protected Properties
    // =========================================================================

    /**
     * @var    bool|array Allows anonymous access to this controller's actions.
     *         The actions must be in 'kebab-case'
     * @access protected
     */
    protected $allowAnonymous = ['index', 'signup'];

    // Public Methods
    // =========================================================================

    /**
     * Handle a request going to our plugin's index action URL,
     * e.g.: actions/poptin/default
     *
     * @return mixed
     */
    public function actionIndex()
    {
        $result = 'Welcome to the DefaultController actionIndex() method';

        return $result;
    }

    /**
     * Handle a request going to our plugin's actionDoSomething URL,
     * e.g.: actions/poptin/default/do-something
     *
     * @return mixed
     */
    public function actionSignup()
    {
        $result = 'Welcome to the DefaultController actionSignup() method';

        return $result;
    }
}
</code></pre>

<p>I see this error:</p>

<p><a href="https://i.sstatic.net/9bO9X.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/9bO9X.png" alt="Error"></a></p>

CC BY-SA 4.0
2020-06-15T13:26:24.103Z
You are using `EVENT_REGISTER_CP_URL_RULES` which only registers control panel routes. If you are creating a route for the outside of the control panel, you should use `EVENT_REGISTER_SITE_URL_RULES`.

See the documentation for more information here: https://docs.craftcms.com/api/v3/craft-web-urlmanager.html#events
Jason McCallister
CC BY-SA 4.0
2020-06-15T13:32:45.187Z
So what I basically want to do is when a user clicks something on my plugin page, I want to hit that route and run some php code in craft. What should I use? `EVENT_REGISTER_CP_URL_RULES` or `EVENT_REGISTER_SITE_URL_RULES`
Rohan
CC BY-SA 4.0
2020-06-15T13:36:36.867Z
`EVENT_REGISTER_SITE_URL_RULES` is triggered when registering URL rules for the front-end site (from the documentation link). For your scenario, that would be the correct event.
Jason McCallister
<plugin-development><craft3><php><routing>
Register custom REST routes while building a plugin for Craft 3
# Register custom REST routes while building a plugin for Craft 3

(Asked by: username_0 on 2020-06-15)

I am building a custom plugin for Craft and I am hitting some REST APIs in my backend service to fetch some data using Ajax calls in JS. What I would like to do when the AJAX calls complete and come back with data, is to store some data in Craft. I know I can use projectConfig to store and retrieve plugin based data and settings but how to call those Craft functions through my JS files. The way I figured is to call it using REST routes that I can define via my plugin.
But I am not sure how to register routes using my plugin and how to setup controllers for the same.
Main plugin file:
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
            $event->rules['poptin/signup'] = 'poptin/default/signup';
        });

DefaultController.php

I see this error:

**Tags:** plugin-development, craft3, php, routing
> **Comments:**
> **username_1**: You are using `EVENT_REGISTER_CP_URL_RULES` which only registers control panel routes. If you are creating a route for the outside of the control panel, you should use `EVENT_REGISTER_SITE_URL_RULES`.  See the documentation for more information here: https://docs.craftcms.com/api/v3/craft-web-urlmanager.html#events  
> **username_0**: So what I basically want to do is when a user clicks something on my plugin page, I want to hit that route and run some php code in craft. What should I use? `EVENT_REGISTER_CP_URL_RULES` or `EVENT_REGISTER_SITE_URL_RULES`  
> **username_1**: `EVENT_REGISTER_SITE_URL_RULES` is triggered when registering URL rules for the front-end site (from the documentation link). For your scenario, that would be the correct event.  

---

## 1 answers

### Answer by username_1 on 2020-06-15. Score: 2 | Accepted answer

Your code is almost there, Craft allows registering routes for the Control Panel and the front-end of the site. You need to change `EVENT_REGISTER_CP_URL_RULES` to `EVENT_REGISTER_SITE_URL_RULES`.

You are listening for an event on `EVENT_REGISTER_CP_URL_RULES` which will only register routes inside the control panel. This is useful for plugins that want to add controllers for settings and custom logic.

> The event that is triggered when registering URL rules for the control panel.

<https://docs.craftcms.com/api/v3/craft-web-urlmanager.html#events>

However, in the scenario where access is needed outside of the control panel, Craft triggers the `EVENT_REGISTER_SITE_URL_RULES`. This allows access for users that do not have control panel access (e.g. site guests and regular users).

> The event that is triggered when registering URL rules for the front-end site.

> **Comments:**
> **username_0**: Worked like a charm! Thanks!  

---

