Creating Nodes is as simple as creating other entities in Drupal 8. When you have Pathauto installed, you can also create a custom alias and pass a flag for Pathauto so it does not overwrite it with current set of rules.
This can be useful when you need to programmatically create nodes that must have a strict URL that doesn't match the Pathauto patterns.
use Drupal\node\Entity\Node; use Drupal\pathauto\PathautoState; Node::create([ 'uid' => 1, 'title' => 'Test page', 'status' => 1, 'type' => 'page', 'body' => [ 'value' => 'Test page with a custom alias', 'format' => 'full_html', ], 'path' => [ 'alias' => '/custom-alias', 'pathauto' => PathautoState::SKIP, ], ])->save();