Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Yii

You're reading from   Mastering Yii Advance your modern web application development skills with Yii Framework 2

Arrow left icon
Product type Paperback
Published in Jan 2016
Publisher Packt
ISBN-13 9781785882425
Length 380 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Charles R. Portwood ll Charles R. Portwood ll
Author Profile Icon Charles R. Portwood ll
Charles R. Portwood ll
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Composer, Configuration, Classes, and Path Aliases FREE CHAPTER 2. Console Commands and Applications 3. Migrations, DAO, and Query Building 4. Active Record, Models, and Forms 5. Modules, Widgets, and Helpers 6. Asset Management 7. Authenticating and Authorizing Users 8. Routing, Responses, and Events 9. RESTful APIs 10. Testing with Codeception 11. Internationalization and Localization 12. Performance and Security 13. Debugging and Deploying Index

Path aliases

In Yii2, path aliases are used to represent file paths or URL paths so that we don't hardcode paths or URLs directly into our application. In Yii2, aliases always start with the @ symbol so that Yii knows how to differentiate it from a file path or URL.

Aliases can be defined in several ways. The most basic way to define a new alias is to call \Yii::setAlias():

\Yii::setAlias('@path', '/path/to/example');
\Yii::setAlias('@example, 'https://www.example.com');

Aliases can also be defined in the application configuration file by setting the alias option as follows:

return [
    // ...
    'aliases' => [
        '@path => '/path/to/example,
        '@example' => 'https://www.example.com',
    ],
];

Also, aliases can be easily retrieved using \Yii::getAlias():

\Yii::getAlias('@path') // returns /path/to/example
\Yii::getAlias('@example') // returns https://www.example.com

Several places in Yii are alias-aware and will accept aliases as inputs. For example, yii\caching\FileCache accepts a file alias as an alias for the $cachePath parameter:

$cache = new FileCache([
    'cachePath' => '@runtime/cache',
]);

Note

For more information on path aliases, check out the Yii documentation at http://www.yiiframework.com/doc-2.0/guide-concept-aliases.html.

You have been reading a chapter from
Mastering Yii
Published in: Jan 2016
Publisher: Packt
ISBN-13: 9781785882425
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image