Category: Yii

  • Creating Extensions

    Let us create a simple extension displaying a standard “Hello world” message. This extension will be distributed via the Packagist repository. Step 1 − Create a folder called hello-world in your hard drive but not inside the Yii basic application template). Inside the hello-world directory, create a file named composer.json with the following code. We have declared that we are using the…

  • Extensions

    Extensions are packages specifically designed to be used in Yii applications. You can share your own code as an extension or use third-party extensions to add features to your application. Using Extensions Most extensions are distributed as Composer packages. Composer installs packages from Packagist – the repository for Composer packages. To install a third-party extension,…

  • Asset Conversion

    Instead of writing CSS or JS code, developers often use extended syntax, like LESS, SCSS, Stylus for CSS and TypeScript, CoffeeScript for JS. Then they use special tools to convert these files into real CSS and JS. The asset manager in Yii converts assets in extended syntax into CSS and JS, automatically. When the view is rendered, it will include the…

  • Assets

    An asset is a file (css, js, video, audio or image, etc.) that may be referenced in a web page. Yii manages assets in asset bundles. The purpose of an asset bundle is to have a group of related JS or CSS files in the code base and to be able to register them within a single PHP call. Asset…

  • Layouts

    Layouts represent the common parts of multiple views i.e. for example, page header and footer. By default, layouts should be stored in the views/layouts folder. Let us have a look at the main layout of the basic application template − This layout generates the HTML page that is common for all pages. The $content variable is the rendering result…

  • Views

    Views are responsible for presenting the data to end users. In web applications, Views are just PHP script files containing HTML and PHP code. Creating Views Step 1 − Let us have a look at the ‘About’ view of the basic application template. The $this variable refers to the view component that manages and renders this view template. This is how the ‘About’ page…

  • Modules

    A module is an entity that has its own models, views, controllers, and possibly other modules. It is practically an application inside the application. Step 1 − Create a folder called modules inside your project root. Inside the modules folder, create a folder named hello. This will be the basic folder for our Hello module. Step 2 − Inside the hello folder,…

  • Widgets

    A widget is a reusable client-side code, which contains HTML, CSS, and JS. This code includes minimal logic and is wrapped in a yii\base\Widget object. We can easily insert and apply this object in any view. Step 1 − To see widgets in action, create an actionTestWidget function in the SiteController with the following code. In the above example, we just returned…

  • Models

    Models are objects representing business logic and rules. To create a model, you should extend the yii\base\Model class or its subclasses. Attributes Attributes represent the business data. They can be accessed like array elements or object properties. Each attribute is a publicly accessible property of a model. To specify what attributes a model possesses, you should override…

  • Using Actions

    To create an action in a controller class, you should define a public method whose name starts with the word action. The return data of an action represents the response to be sent to the end user. Step 1 − Let us define the hello-world action in our ExampleController. Step 2 − Type http://localhost:8080/index.php?r=example/hello-world in the address bar of the…