How to pass a custom object from a page to another page through a route or other means? I want to be able to pass a custom object of type FormMode through a route or any other way.

My custom.routing.yml file is:

custom.page:
  path: '/custom/{model}'
  defaults:
    _controller: '\Drupal\custom\Controller\CustomController::page'
    model: ''
  requirements:
    _permission: 'administer site configuration' 


My FormModel class looks like:

class FormModel {
  public $name;
  public $area;

  function __construct($name, $area) {
    $this-&gt;name = $name;
    $this-&gt;area = $area;
  }


I create an instance of the object and use Drupal URL function like shown below:

$model = new FormModel('name', 'area');

Url::fromRoute('custom.page')-&gt;setRouteParameters(array('model' =&gt; $model));


but Drupal throws an error saying Drupal cannot convert a object to a string which I understand it cant convert object to string.

What would be the best practice to pass a object through a route or pass object created from a form into a controller? I dont want to create an entity as I would then need to update the database with the new fields.

Thanks
You are passing the object, but you need to pass the object ID. So you will change this:

Url::fromRoute('custom.page')-&gt;setRouteParameters(array('model' =&gt; $model));


To this:

Url::fromRoute('custom.page')-&gt;setRouteParameters(array('model' =&gt; $model-&gt;id()));


Note that your model object will need to have a public id() method that returns the ID for this to work. This will send the ID that is passed in place of {model} to the callback.

If you want the callback function (the controller) to receive a loaded object rather than just the ID, you'll need to do a few things. The first is to add options to your route:

custom.page:
  path: '/custom/{model}'
  defaults:
    _controller: '\Drupal\custom\Controller\CustomController::page'
    model: ''
  requirements:
    _permission: 'administer site configuration'
  options:
    parameters:
      model:
        type: model


Then you'll need to set up a new route in services.yml:

services:
  model:
    class: Drupal\mymodule\ParamConverter\MyModuleParamConverter
    tags:
      - { name: paramconverter }


And you'll need to create the class (Drupal\mymodule\ParamConverter\MyModuleParamConverter) that does the conversion from ID to object. This class needs to implement ParamConverterInterface: 

namespace Drupal\mymodule\ParamConverter;

use Drupal\Core\ParamConverter\ParamConverterInterface;
use Symfony\Component\Routing\Route;

class MyModuleParamConverter implements ParamConverterInterface {
  public function convert($value, $definition, $name, array $defaults) {
    // Create your Model object here (not shown) then return it:
    return $model;
  }

  public function applies($definition, $name, Route $route) {
    return (!empty($definition['type']) &amp;&amp; $definition['type'] == 'model');
  }


}

You can read more about parameter upcasting here: https://www.drupal.org/docs/8/api/routing-system/parameter-upcasting-in-routes