Silex简介-Symfony微型框架

tech2022-09-02  111

Silex is a PHP micro-framework based on Symfony components and inspired by the Sinatra Ruby framework. In this article, we are going to get started with the framework and see the how it fits our needs.

Silex是一个基于Symfony组件PHP微框架,其灵感来自Sinatra Ruby框架。 在本文中,我们将开始使用该框架,并了解它如何满足我们的需求。

安装 (Installation)

The best and recommended way to install Silex is through composer:

推荐的最佳安装Silex的方法是通过composer:

// composer.json { "require": { "silex/silex": "1.3.*@dev", "twig/twig": "1.17.*@dev" }, "require-dev": { "symfony/var-dumper": "dev-master" } }

Run composer update --dev to load the dependencies and generate the autoloader. We also required twig because we want to use it as our template engine, and the new var-dumper from Symfony as a development dependency – read more about it here.

运行composer update --dev以加载依赖项并生成自动加载器。 我们还需要twig因为我们想将其用作模板引擎,并将Symfony的新var-dumper dumper用作开发依赖项– 在此处了解更多信息。

创建文件夹结构 (Creating a Folder Structure)

One of the things I like about Silex is that it gives you a bare bones framework that you can organize in any way you want.

我喜欢Silex的一件事是,它为您提供了一个裸露的框架,您可以用任何想要的方式来组织它。

|-app/ |----config/ |-resources/ |----views/ |----logs/ |-src/ |----MyApp/ |-public/ |----index.php |----.htaccess |-vendor/ |-composer.json

For instance, I don’t like my root application folder to be called web, I prefer the normal public folder. The src directory is where we put our application specific code while the other folders are rather self explanatory.

举例来说,我不喜欢我的根应用程序文件夹被称为web ,我更喜欢正常的public文件夹。 src目录是我们放置应用程序特定代码的位置,而其他文件夹则很容易解释。

Our public/index.php file will create a new Silex\Application which is our app container instance, and this is where we are going to wire the components.

我们的public/index.php文件将创建一个新的Silex\Application ,这是我们的应用程序容器实例,这是我们连接组件的地方。

// public/index.php <?php require_once __DIR__ . '/../vendor/autoload.php'; $app = new Silex\Application(); $app->run();

Now if you hit your root application URL, you should see a page not found error. You can turn on debugging by setting the debug mode in the container to true.

现在,如果您击中根应用程序URL,您应该会看到页面未找到错误。 您可以通过将容器中的debug模式设置为true来打开调试。

If you are having problems accessing your routes, make sure that your server document root is pointing to the public folder. You can check the doc for more info about configuring your webserver.

如果访问路由时遇到问题,请确保服务器文档根目录指向公用文件夹。 您可以检查文档以获取有关配置Web服务器的更多信息。

// public/index.php //... $app['debug'] = true; $app->run();

Now if we try again we get a descriptive NotFoundHttpException, because we didn’t register any routes.

现在,如果我们再试一次,则会得到一个描述性的NotFoundHttpException ,因为我们没有注册任何路由。

路由 (Routing)

Registering a route is straightforward, you simply map a URL pattern to a controller function.

注册路由非常简单,您只需将URL模式映射到控制器功能即可。

// public/index.php $app->get('/', function(){ return "Hello world"; });

We can handle get, post, put and delete, or we can use the match method to handle any request method. The handle function must return either a string or a Symfony\Component\HttpFoundation\Response instance.

我们可以处理get , post , put和delete ,也可以使用match方法处理任何请求方法。 handle函数必须返回字符串或Symfony\Component\HttpFoundation\Response实例。

// public/index.php $app->get('/', function(){ return new Symfony\Component\HttpFoundation\Response("Hello world"); });

路由参数 (Routing Parameters)

$app->get("/users/{id}", function($id){ return "User - {$id}"; });

We can add multiple parameters to the URL pattern, the only convention is that the name of the URL pattern parameter must match the name used in the function, or you’ll get a RuntimeException. You can also specify a URL pattern using the assert method, and a default value using the value method.

我们可以向URL模式添加多个参数,唯一的约定是URL模式参数的名称必须与函数中使用的名称匹配,否则您将获得RuntimeException 。 您还可以使用assert方法指定URL模式,并使用value方法指定默认值。

$app->get("/users/{id}", function($id){ return "User - {$id}"; }) ->value("id", 0) //set a default value ->assert("id", "\d+"); // make sure the id is numeric

One of my favorite route methods is convert: it allows us to intercept the request and change the parameter value before passing it to the callback function.

我最喜欢的路由方法之一是convert :它使我们可以拦截请求并更改参数值,然后再将其传递给回调函数。

$app->get("/users/{user}", function($user){ // return the user profile return "User {$user}"; })->convert("user", function($id){ $userRepo = new User(); $user = $userRepo->find($id); if(!$user){ return new Response("User #{$id} not found.", 404); } return $user; });

In this example, the convert method takes a user id, looks up the database and returns the user. A 404 response is returned if the user is not found.

在此示例中, convert方法获取用户标识,查找数据库并返回用户。 如果找不到用户,则返回404响应。

If you are a Laravel fan, you are used to filters like auth, csrf and guest. In Silex, however, you can provide a callback function to behave like a filter.

如果您是Laravel粉丝,则习惯于使用auth , csrf和guest等过滤器。 但是,在Silex中,您可以提供一个回调函数,使其行为类似于过滤器。

$app->get("/users/{user}", function($user){ // return the user profile return "User {$user}"; })->before(function($request, $app){ // redirect if the user is not logged in }) ->after(function($request, $response){ // log request events }) ->finish(function(){ // log request event });

Similarly, you can use the after and finish methods. But keep in mind that the finish method doesn’t have access to the request and response because the response is already sent to the user.

同样,您可以使用after和finish方法。 但是请记住,由于已将响应发送给用户,所以finish方法无法访问request和response 。

命名路线 (Named Routes)

When dealing with multiple routes it makes sense to name them descriptively. This can be helpful when updating the URL format or generating template links.

在处理多条路线时,以描述性的方式命名它们是有意义的。 在更新URL格式或生成模板链接时,这将很有帮助。

$app->get("/users/list", function(Silex\Application $app){ return "List of users"; })->bind('users');

If you are using the URL Generator Service Provider, you can generate a link directly to the route.

如果您使用的是URL Generator服务提供商 ,则可以直接生成指向该路线的链接。

<a href="{{ app.url_generator.generate('users') }}">Users</a>

控制器 (Controllers)

In real world applications, we don’t use closures for routing, but rather create separate controller classes to handle the requests.

在现实世界的应用程序中,我们不使用闭包进行路由,而是创建单独的控制器类来处理请求。

$app->get("/", "MyApp\Controller\HomeController::index");

分组控制器 (Grouping Controllers)

One of the main benefits of using class controllers is the ability to group them. When creating a RESTful API, the URL’s will be something like this:

使用类控制器的主要好处之一是可以对它们进行分组。 创建RESTful API时,URL如下所示:

/users

/users

/users/id [PUT, DELETE]

/users/id [PUT,DELETE]

/users/id/edit

/users/id/edit

A really clean way to deal with this is to group the controllers into something called controller providers. Our User controller provider must implement the ControllerProviderInterface and define the connect method.

解决此问题的一种真正干净的方法是将控制器分为称为控制器提供程序的某种东西。 我们的User控制器提供者必须实现ControllerProviderInterface并定义connect方法。

// src/MyApp/Controller/Provider/User.php class User implements ControllerProviderInterface{ public function connect(Application $app) { $users = $app["controllers_factory"]; $users->get("/", "MyApp\\Controller\\UserController::index"); $users->post("/", "MyApp\\Controller\\UserController::store"); $users->get("/{id}", "MyApp\\Controller\\UserController::show"); $users->get("/edit/{id}", "MyApp\\Controller\\UserController::edit"); $users->put("/{id}", "MyApp\\Controller\\UserController::update"); $users->delete("/{id}", "MyApp\\Controller\\UserController::destroy"); return $users; } }

The $app['controllers_factory'] returns a new Silex\ControllerCollection which holds our routing collection. The UserController will handle the registered requests.

$app['controllers_factory']返回一个新的Silex\ControllerCollection ,其中包含我们的路由集合。 UserController将处理已注册的请求。

// src/MyApp/Controller/UserController.php class UserController{ public function index(){ // show the list of users } public function edit($id){ // show edit form } public function show($id){ // show the user #id } public function store(){ // create a new user, using POST method } public function update($id){ // update the user #id, using PUT method } public function destroy($id){ // delete the user #id, using DELETE method } }

The only remaining part is to attach our controller collection to our application. When working with routes, I prefer the Laravel approach of registering them inside a separate file and including them.

剩下的唯一部分是将我们的控制器集合附加到我们的应用程序。 使用路由时,我更喜欢Laravel方法,将它们注册在一个单独的文件中并包括它们。

// app/routes.php $app->mount("/users", new \MyApp\Controller\Provider\User());

The mount method takes the prefix and our User provider class as parameters.

mount方法将前缀和我们的User provider类作为参数。

Additionally, one of the benefits of using controller collections is the ability to use the before, after and finish filters without having to call them on every route.

此外,使用控制器集合的好处之一是能够使用before , after和finish过滤器,而不必在每个路径上都调用它们。

// src/MyApp/Controller/Provider/User.php class User implements ControllerProviderInterface{ public function connect(Application $app) { //... $users->before(function(){ // check for something here }); } }

提供者 (Providers)

We mentioned this term before but it simply stands for a small class that ties a component to the Silex application. There is a list of pre-included providers and, to use one, you simply register it to the application instance.

我们之前提到过这个术语,但它只是代表将组件与Silex应用程序联系在一起的一小类。 这里有一个预先提供的提供程序列表,要使用其中一个,只需将其注册到应用程序实例中即可。

// app/providers.php $app->register(new Silex\Provider\TwigServiceProvider(), array( 'twig.path' => __DIR__.'/../views', ));

Of course this should be done after requiring Twig inside your composer and updating dependencies. Now if you visit the TwigServiceProvider::register method you can see that we can access the Twig_Environment from the container.

当然,这需要在作曲家内部要求Twig并更新依赖项之后才能完成。 现在,如果您访问TwigServiceProvider::register方法,您会看到我们可以从容器访问Twig_Environment 。

$app->get(function(){ return $app['twig']->render('home.twig'); });

To create your own provider for Silex, you need to implement the Silex\ServiceProviderInterface and define the register method. You can read more in the doc.

要为Silex创建自己的提供程序,您需要实现Silex\ServiceProviderInterface并定义register方法。 您可以在doc中阅读更多内容。

结论 (Conclusion)

Silex is a small and fast framework for Symfony fans. This introduction is aimed at getting you on board and trying the framework. We didn’t cover everything but hopefully I can make you exited to give it a go. If you have any questions or opinions let me know in the comments!

Silex是Symfony粉丝的小型快速框架。 本简介旨在使您入门并尝试该框架。 我们没有涵盖所有内容,但希望我可以让您退出以尝试一下。 如果您有任何疑问或意见,请在评论中告诉我!

翻译自: https://www.sitepoint.com/introduction-silex-symfony-micro-framework/

最新回复(0)