mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 14:28:27 +08:00
@ -175,6 +175,7 @@ Special Topics
|
||||
* [Shared Hosting Environment](tutorial-shared-hosting.md)
|
||||
* [Template Engines](tutorial-template-engines.md)
|
||||
* [Working with Third-Party Code](tutorial-yii-integration.md)
|
||||
* [Using Yii as a micro framework](tutorial-yii-as-micro-framework.md)
|
||||
|
||||
|
||||
Widgets
|
||||
|
@ -1,10 +1,10 @@
|
||||
# Using Yii as a Micro-framework
|
||||
|
||||
Yii can be easily used without features included in basic and advanced templates. In other words, Yii is already a micro-framework. It is not required to have the directory structure provided by templates to work with Yii.
|
||||
Yii can be easily used without the features included in basic and advanced templates. In other words, Yii is already a micro-framework. It is not required to have the directory structure provided by templates to work with Yii.
|
||||
|
||||
This is especially handy when you do not need all the pre-defined template code like assets or views. One of such cases is building a JSON API. In the following sections will show how to do that.
|
||||
|
||||
## Create Project
|
||||
## Installing Yii
|
||||
|
||||
Create a directory for your project files and change working directory to that path. Commands used in examples are Unix-based but similar commands exist in Windows.
|
||||
|
||||
@ -13,7 +13,7 @@ mkdir micro-app
|
||||
cd micro-app
|
||||
```
|
||||
|
||||
> Note: Composer knowledge is required to continue. If you don't know how to use composer yet, please take time to read [Composer Guide](https://getcomposer.org/doc/00-intro.md).
|
||||
> Note: A little bit of Composer knowledge is required to continue. If you don't know how to use composer yet, please take time to read [Composer Guide](https://getcomposer.org/doc/00-intro.md).
|
||||
|
||||
Create file `composer.json` under the `micro-app` directory using your favorite editor and add the following:
|
||||
|
||||
@ -31,11 +31,11 @@ Create file `composer.json` under the `micro-app` directory using your favorite
|
||||
}
|
||||
```
|
||||
|
||||
Save the file and run the `composer install` command. This will install the framework with all its dependencies. Alternatively you can skip the manual creation of the `composer.json` file and just run the command `composer require yiisoft/yii2`. This will create the `composer.json` file as well as install the dependencies.
|
||||
Save the file and run the `composer install` command. This will install the framework with all its dependencies.
|
||||
|
||||
## Create Project Structure
|
||||
## Creating the Project Structure
|
||||
|
||||
After you have installed the framework, it's time to create an entry point for the application. Entry point is the very first file that will be executed when you try to open your application. For the security reasons, it is recommended to put the entrypoint file in a separate directory and make it a web root.
|
||||
After you have installed the framework, it's time to create an [entry point](structure-entry-scripts.md) for the application. Entry point is the very first file that will be executed when you try to open your application. For the security reasons, it is recommended to put the entrypoint file in a separate directory and make it a web root.
|
||||
|
||||
Create a `web` directory and put `index.php` inside with the following content:
|
||||
|
||||
@ -46,37 +46,114 @@ Create a `web` directory and put `index.php` inside with the following content:
|
||||
defined('YII_DEBUG') or define('YII_DEBUG', true);
|
||||
defined('YII_ENV') or define('YII_ENV', 'dev');
|
||||
|
||||
require(__DIR__ . '../vendor/autoload.php');
|
||||
require(__DIR__ . '../vendor/yiisoft/yii2/Yii.php');
|
||||
|
||||
// you may put this into a separate file
|
||||
$config = [
|
||||
'id' => 'micro-app',
|
||||
'basePath' => __DIR__,
|
||||
'controllerNamespace' => 'micro\controllers',
|
||||
'aliases' => [
|
||||
'@micro' => dirname(__DIR__),
|
||||
]
|
||||
];
|
||||
require(__DIR__ . '/../vendor/autoload.php');
|
||||
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
|
||||
|
||||
$config = require __DIR__ . '/../config.php';
|
||||
(new yii\web\Application($config))->run();
|
||||
```
|
||||
|
||||
> Note: Config can be moved to a separate file located in a separate directory.
|
||||
|
||||
Your project is ready for coding. Although it's up to you to decide the project directory structure, as long as you observe namespaces.
|
||||
|
||||
## Create Necessary Directories
|
||||
|
||||
In order to demonstrate the usage of our "micro framework", we will create simple REST API for posts.
|
||||
|
||||
Create directories `models` and `controllers` respectively.
|
||||
|
||||
> Note: In our example, we have changed default application namespace `app` to a `micro` to demonstrate that you are not tied to that name (in case you thought you were), then adjusted [controllers namespace](http://www.yiiframework.com/doc-2.0/yii-base-application.html#$controllerNamespace-detail) and set the correct alias.
|
||||
|
||||
Create `Post.php` file in the `models` directory. This is the code for the model.
|
||||
Also create a file named `config.php` which will contain all application configuration:
|
||||
|
||||
```php
|
||||
<?php
|
||||
return [
|
||||
'id' => 'micro-app',
|
||||
// the basePath of the application will be the `micro-app` directory
|
||||
'basePath' => __DIR__,
|
||||
// this is where the application will find all controllers
|
||||
'controllerNamespace' => 'micro\controllers',
|
||||
// set an alias to enable autoloading of classes from the 'micro' namespace
|
||||
'aliases' => [
|
||||
'@micro' => __DIR__,
|
||||
],
|
||||
];
|
||||
```
|
||||
|
||||
> Info: Even though the configuration could be kept in the `index.php` file it is recommended
|
||||
> to have it separately. This way it can be used for console application also as it is shown below.
|
||||
|
||||
Your project is now ready for coding. Although it's up to you to decide the project directory structure, as long as you observe namespaces.
|
||||
|
||||
## Creating the first Controller
|
||||
|
||||
Create a `controllers` directory and add a file `SiteController.php`, which is the default
|
||||
controller that will handle a request with no path info.
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace micro\controllers;
|
||||
|
||||
use yii\web\Controller;
|
||||
|
||||
class SiteController extends Controller
|
||||
{
|
||||
public function actionIndex()
|
||||
{
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
If you want to use a different name for this controller you can change it and configure [[yii\base\Application::$defaultRoute]] accordingly.
|
||||
For example, for a `DefaultController` that would be `'defaultRoute' => 'default/index'`.
|
||||
|
||||
At this point the project structure should look like this:
|
||||
|
||||
```
|
||||
micro-app/
|
||||
├── composer.json
|
||||
├── web/
|
||||
└── index.php
|
||||
└── controllers/
|
||||
└── SiteController.php
|
||||
```
|
||||
|
||||
If you have not set up the web server yet, you may want to take a look at [web server configuration file examples](start-installation.md#configuring-web-servers).
|
||||
Another options is to use the `yii serve` command which will use the PHP build-in web server. You can run
|
||||
it from the `micro-app/` directory via:
|
||||
|
||||
vendor/bin/yii serve --docroot=./web
|
||||
|
||||
Opening the application URL in a browser should now print "Hello World!" which has been returned in the `SiteController::actionIndex()`.
|
||||
|
||||
> Info: In our example, we have changed default application namespace `app` to `micro` to demonstrate
|
||||
> that you are not tied to that name (in case you thought you were), then adjusted
|
||||
> [[yii\base\Application::$controllerNamespace|controllers namespace]] and set the correct alias.
|
||||
|
||||
|
||||
## Creating a REST API
|
||||
|
||||
In order to demonstrate the usage of our "micro framework", we will create a simple REST API for posts.
|
||||
|
||||
For this API to serve some data, we need a database first. Add the database connection configuration
|
||||
to the application configuration:
|
||||
|
||||
```php
|
||||
'components' => [
|
||||
'db' => [
|
||||
'class' => 'yii\db\Connection',
|
||||
'dsn' => 'sqlite:@micro/database.sqlite',
|
||||
],
|
||||
],
|
||||
```
|
||||
|
||||
> Info: We use an sqlite database here for simplicity. Please refer to the [Database guide](db-dao.md) for more options.
|
||||
|
||||
Next we create a [database migration](db-migrations.md) to create a posts table.
|
||||
Make sure you have a separate configuration file as explained above, we need it to run the console commands below.
|
||||
Running the following commands will
|
||||
create a database migration file and apply the migration to the database:
|
||||
|
||||
vendor/bin/yii migrate/create --appconfig=config.php create_post_table --fields="title:string,body:text"
|
||||
vendor/bin/yii migrate/up --appconfig=config.php
|
||||
|
||||
Create directory `models` and a `Post.php` file in that directory. This is the code for the model:
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
namespace micro\models;
|
||||
|
||||
use yii\db\ActiveRecord;
|
||||
@ -85,35 +162,49 @@ class Post extends ActiveRecord
|
||||
{
|
||||
public static function tableName()
|
||||
{
|
||||
return '{{post}}';
|
||||
return '{{posts}}';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Create `ApiController` in `controllers`.
|
||||
> Info: The model created here is an ActiveRecord class, which represents the data from the `posts` table.
|
||||
> Please refer to the [active record guide](db-active-record.md) for more information.
|
||||
|
||||
To serve posts on our API, add the `PostController` in `controllers`:
|
||||
|
||||
```php
|
||||
namespace micro\resources;
|
||||
<?php
|
||||
|
||||
namespace micro\controllers;
|
||||
|
||||
use yii\rest\ActiveController;
|
||||
|
||||
class ApiController extends ActiveController
|
||||
class PostController extends ActiveController
|
||||
{
|
||||
public $modelClass = 'micro\models\Post';
|
||||
|
||||
public function behaviors()
|
||||
{
|
||||
// remove rateLimiter which requires an authenticated user to work
|
||||
$behaviors = parent::behaviors();
|
||||
unset($behaviors['rateLimiter']);
|
||||
return $behaviors;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
So the project structure should look like this:
|
||||
At this point our API will provide the following URLs:
|
||||
|
||||
```
|
||||
micro-app/
|
||||
├── composer.json
|
||||
├── web
|
||||
└── index.php
|
||||
├── models
|
||||
└── Post.php
|
||||
└── resources
|
||||
└── ApiController.php
|
||||
```
|
||||
- `/index.php?r=post` - list all posts
|
||||
- `/index.php?r=post/view&id=1` - show post with ID 1
|
||||
- `/index.php?r=post/create` - create a post
|
||||
- `/index.php?r=post/update&id=1` - update post with ID 1
|
||||
- `/index.php?r=post/delete&id=1` - delete post with ID 1
|
||||
|
||||
Now you can use the application on REST API calls just like you would for any application made with Yii.
|
||||
Starting from Here you may want to look at the following guides to further develop your application:
|
||||
|
||||
- The API currently only understands urlencoded form data as input, to make it a real JSON API, you
|
||||
need to configure [[yii\web\JsonParser]].
|
||||
- To make the URLs more friendly you need to configure routing.
|
||||
See [guide on REST routing](rest-routing.md) on how to do this.
|
||||
- Please also refer to the [Looking Ahead](start-looking-ahead.md) section for further references.
|
||||
|
Reference in New Issue
Block a user