Skip to content

Creating a Project

How crest new writes an MVC or an ADR project, its options, and the setup script for a host with docker only.

crest new creates a project. It writes an MVC or an ADR project that runs:

crest new my-app
crest new my-app --flavor=adr

An MVC project has public/index.php (the web entry point) and the front controller src/AppFront.php. It also has crest.php, composer.json, the docker files, and src/Controllers/IndexController.php, which answers GET /. For more, see The MVC project. An ADR project has the same entry point, front controller and files, and src/Action/Get.php, which answers GET /.

new takes the options in new of the Command reference. The view engine (--template-engine) renders the views of an MVC project: with phtml, the default, the views are PHP templates in .phtml files, and with volt, they are Volt templates in .volt files (see Volt). With none, the project has no views (see The MVC project). --camel-case and --get-set change the models that make:model writes from the database tables (see Generating models). They set the models key of crest.php (see Configuration, Names and Getters and setters). --database gives the project a database (see The database).

In a terminal, new asks for each value that no option gives:

  • the flavor
  • the view engine, for MVC
  • the namespace
  • the PHP version
  • the Phalcon version
  • the runtime (host or docker)
  • the service, for docker
  • the database

For an MVC project with a database, new asks two more questions. First, it asks if the property names of the models are in camelCase. Then it asks if the properties are protected, with a getter and a setter.

An empty answer takes the default. --no-interaction (see Global options), or a run without a terminal (CI, a pipe), asks nothing and takes the defaults. The options still give their values, for example in CI.

The project goes into the folder <name> in the working directory, or in --directory if you give one (see Global options). By default, new runs nothing: no composer, no docker, no network. It writes the files and prints the next steps. For an MVC project with no database, Quick start shows the output.

To make new run commands, see Commands after new. To change the files that new writes, see Project stubs.

The MVC project

crest new my-app --flavor=mvc writes the layout of the Phalcon sample applications (vokuro, invo). The table below uses these Phalcon terms:

  • The DI (dependency injection) container of the application holds its services: the objects that the application uses. These are not the docker container and the docker compose service of The runtime.
  • FactoryDefault is the Phalcon DI container with the default services.
  • A provider is a class that registers services in the DI container.
  • Phalcon\Mvc\Application handles each request.
  • The dispatcher loads the controller of a URL and calls it.
Path Contents
public/index.php the web entry point, as in an ADR project
src/AppFront.php the front controller: FactoryDefault, the providers and Phalcon\Mvc\Application
src/Providers/ DispatcherProvider (the default namespace of the controllers), UrlProvider (the base URI of the links and the redirects) and ViewProvider (the views folder and the phtml engine, or Volt: see Volt)
src/Controllers/ IndexController, which answers GET /
src/Controllers/Base/ CrestControllerBase, the base that Crest owns (see Generated bases)
themes/default/ the layout index.phtml and the view index/index.phtml

crest.php has 'flavor' => 'mvc' and the paths (the folders that Crest writes into) command, controller, form, model, module, provider and views (see Configuration). To add a service, run crest make:provider Mailer. It writes src/Providers/MailerProvider.php, a provider that implements Phalcon\Di\ServiceProviderInterface. It also prints the line to add to AppFront::providers(), the list of the providers of the front controller.

With --template-engine=none, the project has no views, for example an application that only returns JSON. new writes no ViewProvider, no layout and no view, and crest.php has 'views' => false. The front controller calls useImplicitView(false): without it, Phalcon\Mvc\Application needs a view service for each request. IndexController returns its response, {"status":"ok"}. In this project, make:scaffold stops, because it writes views. make:module writes no views folder, and make:controller names no view.

A URL with no controller stops in the dispatcher (“handler class cannot be loaded”). With display_errors off, PHP answers 500. With display_errors on, PHP shows the error in the page, with status 200. Add a not-found page if your project needs one. For example, use a dispatcher listener (a class that gets the events of the dispatcher), such as the NotFoundPlugin of invo.

Volt

With --template-engine=volt, the views are Volt templates:

crest new my-app --template-engine=volt

The project then changes in these files:

Path Contents
src/Providers/ViewProvider.php the views folder, and the Volt engine for .volt files. Volt writes the compiled views into var/cache/volt/
themes/default/ the layout index.volt and the view index/index.volt
var/cache/volt/.gitignore keeps the compiled views out of git, and keeps the folder
crest.php 'templateEngine' => 'volt' (see Configuration)

make:scaffold then writes Volt views (see Generating scaffolds).

The database

--database gives the project a database, in each flavor:

crest new my-app --flavor=mvc --database=mysql
Choice The service in docker-compose.yml The PHP extension
none none; the default none
mariadb database, with the image mariadb:11.4 pdo_mysql
mysql database, with the image mysql:8.4 pdo_mysql
pgsql database, with the image postgres:18 pdo_pgsql
sqlite none: the database is the file storage/database.sqlite pdo_sqlite, which the php image has

With a database, new also writes:

  • DbProvider.php in the provider folder (src/Providers for MVC, src/Provider for ADR). DbProvider registers the db service: the connection to the database (see The MVC project for services). In an MVC project, db is a Phalcon\Db adapter, which the models use. In an ADR project, db is a Phalcon\DataMapper\Pdo\Connection, which the repositories use (see Generating repositories). The MVC front controller adds DbProvider in providers(). The ADR front controller gets an override of registerProviders(), the method that registers the services. The override calls DbProvider.

  • The connection values at the end of .env:

    DB_HOST=database
    DB_PORT=3306
    DB_NAME=my_app
    DB_USERNAME=my_app
    DB_PASSWORD=secret
    DB_PUBLISHED_PORT=3306

    DB_NAME is the project name, with _ in place of -, cut to 64 characters. DB_USERNAME is the same name, cut to 32 characters. For sqlite, .env has only DB_NAME=storage/database.sqlite, relative to the project root.

  • For mariadb, mysql and pgsql: the service database in docker-compose.yml, with a named volume for its data and a health check. The app service starts when the database accepts connections. Thus crest make:model works right after crest up and crest install. The database listens on the host only at 127.0.0.1, on the port DB_PUBLISHED_PORT: other computers cannot connect to it.

  • For mariadb, mysql and pgsql: the PDO extension in resources/docker/Dockerfile.

  • For sqlite: the empty file storage/database.sqlite (an empty file is a valid SQLite database), storage/.gitkeep, and /storage/database.sqlite in .gitignore. --force does not replace a database file that exists: git ignores the file, so nothing can restore its data.

docker compose gives the values of .env to the app container. PHP on the host does not read .env. There, DbProvider uses its defaults: 127.0.0.1 and the published port. DbProvider reads each value from getenv(), then from $_ENV, then takes the default.

A loader such as phpdotenv (a PHP library that loads .env) also reads .env on the host. There, DB_HOST=database is not a known host. Then give PHP on the host DB_HOST=127.0.0.1 and DB_PORT (the published port) in its environment: getenv() wins over $_ENV.

To run the project with PHP on the host, start only the database, and install the PDO extension on the host:

docker compose up -d database

If the port is in use on the host, change DB_PUBLISHED_PORT in .env. Then PHP on the host needs DB_PORT with the same value in its environment. The MySQL and MariaDB images do not accept the user root. Thus a project with the name root needs another DB_USERNAME.

The db service is shared. It connects when the project uses it for the first time, for example at the first query of a model. Thus the project answers GET / when the database does not run. A wrong value gives the connection error at the first use of db.

With mysql, mariadb or pgsql, the app service cannot have the name database:

crest: the service 'database' is the name of the database service; choose another service name

In an ADR project with a database, the front controller already has registerProviders(). For a new provider, crest make:provider prints only the provide() line to add to it. provide() is the method of a provider that registers its services. Crest reads the front controller from the bootstrap key of crest.php. When it cannot load that class, it prints the whole override.

To change the service or the .env lines of the database, for example the image, see Project stubs.

To add a database to a project that exists, see Adding a database.

Commands after new

new can run commands after it writes the files, for example crest up and crest install. The new key of crest.php lists these commands. The key and the examples in this section are the same in both flavors. new writes crest.php from the project-config stub (see Stubs). Thus publish the project-config stub in the folder that the projects go into (see Project stubs):

crest stub:publish project-config

Then add the key new to the copy. Each entry of the key is one command, as a list of words:

'new' => [
    ['crest', 'up'],
    ['crest', 'install'],
],

With the host runtime, use ['composer', 'install'] instead. With the docker runtime, composer runs in the container. This is because the host often has no Phalcon, or an older PHP, and then composer install on the host stops.

There is no shell: pipes, && and variables do not work. When the first word is crest, the Crest that runs new also runs the command. Use crest, not ./crest. This is because Crest looks for a relative path from the folder where you type the command, not from the project.

new runs the commands in order, in the project folder. It prints Running: <command> before each one. At the end, it prints Ran 2 commands from the 'new' key of crest.php.

The first command that fails stops the list, and new returns its exit status:

crest: 'composer install' failed with exit status 2; the files in my-app/ stay

A program that is not in the PATH also stops the list, with exit status 1:

crest: 'composer' was not found; install it or add it to the PATH

new reads the key from the crest.php that it writes. Thus it checks the key after it writes the files. If the key is not a list of commands, or if PHP cannot parse the file, new stops before the first command. The files stay. The error names the published stub that new used. Change the stub, not the crest.php of the project. Then run new again with --force:

crest: my-app/crest.php: 'new' entry 2 in crest.php must be a list of strings, e.g. ['crest', 'up']; the files in my-app/ stay; fix /path/to/resources/stubs/shared/project-config.stub and run new again with --force

new also checks that the written crest.php has the flavor of the project. Keep 'flavor' => '{{ flavor }}' in a published project-config. A copy with a fixed flavor stops new in the same way, before the first command.

The key stays in the crest.php of the project. No other command reads it. config:show shows the configuration of the project, but not this key (see Command reference). init uses the same stub: it writes the key, but runs nothing (see Adding Crest to an existing project).

The setup script

The crest-setup script of With Docker only runs crest new <name> in a one-off composer:2 container. Each option after the name goes to new (see the options in Creating a project). The script and its examples are the same in both flavors, except the Next: hint at the end: it names a command for the flavor of the new project. --flavor goes to new as each other option. For example, for a project with Phalcon v6:

curl -fsSL https://phalcon.io/crest-setup | sh -s -- my-app --phalcon v6

In the script, new asks no questions: the options give the values, else the defaults apply. The project goes into the folder <name> in the working directory. The script stops with an error when you give --directory, and when the working directory is a system folder such as /usr. The --help option of the script shows the options of new.

The working directory has the same path in the container. Thus the errors of new show host paths (see The launcher). The script writes your user and group IDs into the project .env. Then it runs ./crest up and ./crest install (see With Docker).

If ./crest up or ./crest install fails, for example because the port is in use, the project stays. The script prints the commands to run after you fix the problem.

With rootless docker, the script writes UID=0 and GID=0 into .env (see The launcher). It also does not change the owner of the project files (no chown).

The script runs on your host. To read it before it runs:

curl -fsSL https://phalcon.io/crest-setup -o crest-setup.sh
less crest-setup.sh
sh crest-setup.sh my-app

https://phalcon.io/crest-setup points to https://raw.githubusercontent.com/phalcon/crest/master/resources/setup.sh.

Do not add a new key to a published project-config stub for the setup command (see Commands after new). The script runs new in a container without docker, so crest up fails there. The script runs ./crest up and ./crest install itself.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close