引入引入jque_重新引入FuelPHP

引入引入jque

As a PHP developer, I have been a consistent user of different PHP frameworks, mostly focusing on CakePHP. Recently, I felt the need to go framework shopping and I have many valid reasons for choosing FuelPHP. It has a built-in modular structure and complete flexibility with emphasis on community. Before Fuel, I was a CakePHP user and just like Cake, Fuel is a huge community driven framework.

作为一个PHP开发人员,我一直是不同PHP框架的一致用户,主要关注CakePHP。 最近,我感到有必要去购买框架,并且我有很多正当理由选择FuelPHP 。 它具有内置的模块化结构,并具有强调社区的完全灵活性。 在Fuel之前,我是CakePHP用户,就像Cake一样,Fuel是一个庞大的社区驱动框架。

FuelPHP

安装FuelPHP框架 (Installation of FuelPHP Framework)

To install FuelPHP, the only thing you need to do is run: curl get.fuelphp.com/oil | sh and create your project with oil create project_name.

要安装FuelPHP,您唯一需要做的就是运行: curl get.fuelphp.com/oil | sh curl get.fuelphp.com/oil | sh并使用oil create project_name您的项目oil create project_name

There will be optional commands such as oil refine install, which makes the necessary directories writable, and finally, do composer update to install the dependencies.

将有可选命令,例如oil refine install ,该命令使必要的目录可写,最后进行composer update以安装依赖项。

什么是FuelPHP油? (What is FuelPHP Oil?)

If you have experience with PHP frameworks, the concept of oil will be completely clear to you. For example, Fuel’s Oil is a Laravel Artisan substitute. Indeed, oil is a command line utility to facilitate quick development, test your application, and run multiple tasks. This will enable you to speed up your development by providing several functions:

如果您有PHP框架的经验,那么oil的概念将对您完全清楚。 例如,燃料油是Laravel Artisan的替代品。 的确,oil是一个命令行实用程序,可帮助快速开发,测试您的应用程序并运行多个任务。 通过提供以下功能,这将使您加快开发速度:

  • Generate: Create MVC components, migrations, etc.

    生成:创建MVC组件,迁移等。
  • Refine: Run tasks such as migrate, and also your own customized tasks.

    改进:运行诸如迁移之类的任务,以及您自己的自定义任务。
  • Package: Install, update and remove packages.

    软件包:安装,更新和删除软件包。
  • Console: Test your code in real-time using an interactive shell.

    控制台:使用交互式外壳实时测试您的代码。
  • Testing: Run PHPUnit tests.

    测试:运行PHPUnit测试。

Read more about oil here.

在此处阅读有关石油的更多信息。

FuelPHP软件包 (FuelPHP Packages)

Fuel has packages which allow you to share packages you build with other people. They can be found on Packagist, same as all Composer packages.

Fuel提供的软件包可让您与他人共享构建的软件包。 它们可以在Packagist找到 ,与所有Composer软件包相同。

There are two ways to install a package. You can do it manually by throwing oil, or use Composer. I personally prefer the Composer approach.

有两种安装软件包的方法。 您可以通过扔油手动进行操作,也可以使用Composer。 我个人更喜欢Composer方法。

You can read more about packages here.

您可以在此处阅读有关软件包的更多信息。

入门 (Getting Started)

As you know, Fuel uses MVC architecture like most other frameworks. Every framework has its own rules for specific mvc parts. In Fuel, controllers are placed in the fuel/app/classes/controller directory, and are prefixed with controller_. Optionally, they should extend the controller class for the full feature set. In Fuel, you can route HTTP requests automatically via some prefix actions, like post and get in defining a method.

如您所知,Fuel与大多数其他框架一样使用MVC架构。 每个框架对于特定的mvc部分都有其自己的规则。 在Fuel中,控制器放置在fuel/app/classes/controller目录中,并以controller_为前缀。 (可选)他们应该为完整功能集扩展控制器类。 在Fuel中,您可以通过一些前缀操作自动路由HTTP请求,例如post和进入定义方法。

Let’s get started working with Fuel basics.

让我们开始使用Fuel基础知识。

Please make sure you enable the following two packages in app/config/config.php :

请确保在app/config/config.php启用以下两个软件包:

'packages'  => array(
    'orm',
    'auth'
)

We will use them in the next section. The default route is hello, which shows your welcome page. If you would like to perform some changes, simply edit fuel/app/config/routes.php:

我们将在下一部分中使用它们。 默认路由是hello ,它显示欢迎页面。 如果您想进行一些更改,只需编辑fuel/app/config/routes.php

return array(
    '_root_'  => 'welcome/index',  // The default route
    '_404_'   => 'welcome/404',    // The main 404 route
    'hello(/:name)?' => array('welcome/hello', 'name' => 'hello')
);

向FuelPHP打个招呼 (Say Hello to FuelPHP)

Now would be a good time to say hello to the world of Fuel. First, create a simple authentication app by means of Simpleauth. Simpleauth refers to a simple authentication system which is included in the auth package. To use Simpleauth, copy fuel/packages/auth/config/auth.php and simpleauth.php to fuel/app/config/. Then, create a database table. I use migrations instead of traditional database operations. I would copy core/config/migrations.php to app/config/migrations.php and run the following command to create a scaffold :

现在将是向Fuel世界问好的好时机。 首先,通过Simpleauth创建一个简单的身份验证应用程序。 Simpleauth是指包含在auth包中的简单身份验证系统。 要使用Simpleauth,复制fuel/packages/auth/config/auth.phpsimpleauth.phpfuel/app/config/ 。 然后,创建一个数据库表。 我使用迁移而不是传统的数据库操作。 我将core/config/migrations.php复制到app/config/migrations.php并运行以下命令来创建一个脚手架:

php oil generate scaffold user username:string password:string email:string profile_fields:text  created_at:string updated_at:string last_login:integer[20]

This will create a file in our app/migrations folder named 001_create_users.php, which I’ve edited as:

这将在我们的app/migrations文件夹中创建一个文件001_create_users.php ,我将其编辑为:

namespace Fuel\Migrations;
class 001_create_users
{
    public function up()
    {
     \DBUtil::create_table('users', array(
            'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true),
            'username' => array('constraint' => 255, 'type' => 'varchar'),
            'password' => array('constraint' => 255, 'type' => 'varchar'),
            'email' => array('constraint' => 255, 'type' => 'varchar'),
            'last_login' => array('constraint' => 20, 'type' => 'int'),
            'profile_fields' => array('constraint' => 255, 'type' => 'varchar'),
            'created_at' => array('constraint' => 255, 'type' => 'varchar'),
            'updated_at' => array('constraint' => 255, 'type' => 'varchar')
        ), array('id'));

        $username = "AwesomeAlireza";
        $password = "@awesomeAlireza@";
        $pass_hash = \Auth::instance()->hash_password($password);
        $email = "Alireza@is-awesome.com";
        $users = \Model_User::forge(array(
            'username' => $username,
            'password' => $pass_hash,
            'email' => $email,
            'profile_fields' => '',
            'last_login' => ''
        ));

        if ($users and $users->save())
            \Cli::write("the user has been created");
         else
            \Cli::write("failed to create user");
    }
    public function down()
    {
        \DBUtil::drop_table('users');
    }
}

To submit this, you just need to run php oil refine migrate.

要提交此文件,您只需要运行php oil refine migrate

If you see this result Migrated to latest version: 1., it means that everything went well. After this, please create a Common controller in app/classes/controller/common.php:

如果您看到此结果,请Migrated to latest version: 1. ,这表示一切正常。 之后,请在app/classes/controller/common.php创建一个通用控制器:

class controller_common extends Controller_Template
{
    public function before()
    {
        parent::before();
        $uri_string = explode('/', Uri::string());
        $this->template->logged_in = false;

        if (count($uri_string)>1 and $uri_string[0] == 'users' and $uri_string[1] == 'login')
            return;
         else 
           {
            if(\Auth::check())
            {
                $user = \Auth::instance()->get_user_id();
                $this->user_id = $user[1];
                $this->template->logged_in = true;
            } 
            else 
                \Response::redirect('/users/login');
           }
    }
}

And the user controller is located in app/classes/controller/users.php:

用户控制器位于app/classes/controller/users.php

class controller_users extends Controller_Common
{
    public function action_index()
    {
        $data['users'] = Model_User::find('all');
        $this->template->title = "Users";
        $this->template->content = View::forge('users/index', $data);
    }

    public function action_login()
    {
        if (Auth::check()) 
            Response::redirect('/');
        $val = Validation::forge('users');
        $val->add_field('username', 'Your username', 'required|min_length[3]|max_length[20]');
        $val->add_field('password', 'Your password', 'required|min_length[3]|max_length[20]');

        if ($val->run())
        {
            $auth = Auth::instance();
            if ($auth->login($val->validated('username'), $val->validated('password')))
            {
                Session::set_flash('notice', 'FLASH: logged in');
                Response::redirect('users');
            } 
            else 
            {
                $data['username'] = $val->validated('username');
                $data['errors'] = 'Wrong username/password. Try again';
            }
        }
        else 
        {
            if ($_POST)
            {
                $data['username'] = $val->validated('username');
                $data['errors'] = 'Wrong username/password combo. Try again';
            } 
            else 
            {
                $data['errors'] = false;
            }
        }

       $this->template->errors = $data['errors'];
       $this->template->content = View::forge('users/login')->set($data);
    }

    public function action_view($id = null)
    {
        $data['user'] = Model_User::find($id);
        $this->template->title = "User";
        $this->template->content = View::forge('users/view', $data);
    }

    public function action_logout()
    {
        Auth::instance()->logout();
        Response::redirect('/');
    }
}

As you can see, the controller extends Controller_Common in order to be restricted by log-in. I’ve also validated my input data in the controller, but it could be in our model, too.

如您所见,该控制器扩展了Controller_Common以便受登录限制。 我也已经在控制器中验证了我的输入数据,但是也可以在我们的模型中。

We’re done with controllers and it’s time to create a view for our app. In Fuel, the view files are located under app/views/CONTROLLERNAME/. The first view we’ll create is app/views/users/login.php:

我们已经完成了控制器的工作,现在该为我们的应用创建视图了。 在Fuel中,视图文件位于app/views/CONTROLLERNAME/ 。 我们将创建的第一个视图是app/views/users/login.php

<h2>Login</h2>

Login to your account using your username and password.

<div class="input required">

    <?php isset($errors) ? $errors : false; ?>

    <?php echo Form::open('users/login'); ?>
     
    <?php echo Form::label('Username', 'username'); ?>

    <?php echo Form::input('username', null, array('size' => 30)); ?>

</div>

<div class="input password required">

    <?php echo Form::label('Password', 'password'); ?>

    <?php echo Form::password('password', null, array('size' => 30)); ?>

</div>

<div class="submit" >
    <?php echo Form::submit('login', 'Login'); ?>
</div>

And now, the index.php file:

现在, index.php文件:

<div><?php echo $user->username; ?></div>
alt

The only thing you need to do now is just navigate to https://127.0.0.1/public/users/login in your browser and you’ll see a page like:

您现在唯一要做的就是在浏览器中导航到https://127.0.0.1/public/users/login ,您将看到类似以下页面:

Congratulations, you’ve created a simple authentication app! The code is also available on Github.

恭喜,您已经创建了一个简单的身份验证应用程序! 该代码在Github上也可用。

结论 (Conclusion)

As you can see, Fuel has greatly simplified the path to web application construction. Each framework has its own advantages, but I hope this post has shown you some of Fuel’s, so that you may give it the chance it deserves in your toolbox.

如您所见,Fuel大大简化了Web应用程序构建的路径。 每个框架都有其自身的优势,但是我希望这篇文章向您展示了Fuel的一些优点,以便您可以在工具箱中获得应有的机会。

Comments? Feedback? Let me know!

注释? 反馈? 让我知道!

翻译自: https://www.sitepoint.com/re-introducing-fuelphp/

引入引入jque

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值