Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Sign in to follow this  
墨香年少

create symfony7.1 project using php8.1

Recommended Posts

1.update composer

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e21205b207c3ff031906575712edab6f13eb0b361f2085f1f1237b7126d785e826a450292b6cfd1d64d92e6563bbde02') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

2.install symfony-cli

wget https://get.symfony.com/cli/installer -O - | bash

3.Create New Symfony Applications

using ui webapp:

symfony new --webapp my_project

just api application

symfony new my_project

需要:

1.删除禁用函数:putenv\proc_open

2.配置git


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites
wget https://getcomposer.org/download/2.6.5/composer.phar
php composer.phar update
php composer.phar require symfony/lock
cp composer.phar /usr/local/bin/composer

 


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites
php composer.phar require symfony/maker-bundle --dev

可以使用如下命令:

php bin/console make:controller DefaultController

 


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites

.env配置:

DATABASE_URL="mysql://root:123456@127.0.0.1:端口/数据库名称?serverVersion=5.7&charset=utf8"

创建数据库

php bin/console doctrine:database:create

 


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites

php composer.phar require security-csrf
php composer.phar require symfony/security-bundle
php bin/console make:user


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Doctrine\ORM\EntityManagerInterface;

class DefaultController extends AbstractController
{
    #[Route('/', name: 'app_default')]
    public function index(UserPasswordHasherInterface $passwordHasher,EntityManagerInterface $entityManager): Response
    {
		/*
		//添加用户
		$user = new \App\Entity\User();
        $plaintextPassword = '123456';

		$user->setUsername("ADM".rand(1111,9999));
        $hashedPassword = $passwordHasher->hashPassword($user,$plaintextPassword);
        $user->setPassword($hashedPassword);
		
		$entityManager->persist($user);
        $entityManager->flush();*/
		
		//验证密码
		$user = $entityManager->getRepository(\App\Entity\User::class)->find(1);
		$ret = $passwordHasher->isPasswordValid($user,'1234567');
		var_dump($ret);die();
        return new Response("uid:".$user->getId());
    }
}

 


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;

class LoginController extends AbstractController
{
	private UserPasswordHasherInterface $passwordHasher;
	private EntityManagerInterface $entityManager;
	
	public function __construct(UserPasswordHasherInterface $_passwordHasher,EntityManagerInterface $_entityManager)
	{
		$this->passwordHasher = $_passwordHasher;
		$this->entityManager = $_entityManager;
	}
	
    #[Route('/login', name: 'app_login')]
    public function index(Request $request): Response
    {
		$username = $request->request->get('username','');
		$password = $request->request->get('password','');
		
		$user = $this->entityManager->getRepository(\App\Entity\User::class)->findOneBy(['username'=>$username]);
		
		$ret = $this->passwordHasher->isPasswordValid($user,$password);
		var_dump($ret);die();
    }
}

自定义检测用户是不是正确


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites

当页面设置需要登录才可以访问,但是没有跳转到登录页面,而是提示:

Full authentication is required to access this resource.

如何设置自动跳转到登录页面呢。

首先设置:

firewalls:
    main:
        entry_point: App\Security\AuthenticationEntryPoint
<?php
// src/Security/AuthenticationEntryPoint.php
namespace App\Security;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;

class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
    public function __construct(private UrlGeneratorInterface $urlGenerator) {}

    public function start(Request $request, AuthenticationException $authException = null): RedirectResponse
    {
        return new RedirectResponse($this->urlGenerator->generate('app_login'));
    }
}

 


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites
#!/usr/bin/python
import os
os.system("php bin/console make:migration");
os.system("php bin/console doctrine:migrations:migrate");
print(" ")
print("============== OK ==============")

python脚本


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites

controller中使用

var_dump($this->getParameter('kernel.debug'));

来获取.env中的 APP_DEBUG


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites
<?php

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpKernel\KernelInterface;

class DefaultController extends BaseController
{
	private $environment;
	
	public function __construct(KernelInterface $kernel)
    {
        $this->environment = $kernel->getEnvironment();
    }
	
    #[Route('/', name: 'app_default')]
    public function index(){return $this->_render();}

	private function _render()
	{
		var_dump( $this->environment );
		die();
	}

}

获取.env中的 APP_ENV


目之所及,皆是回忆,心之所想,皆是过往

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

注册必须使用2-8个中文汉字作为账号