Asked  8 Months ago    Answers:  5   Viewed   5 times

I have an action inside my controller class and I want two different routes like below:

/**
 * Displays a form to create a new entity.
 *
 * @Route("/edit/choose/date", name="user_choose_date")
 * @Route("/supervisory/choose/date", name="sup_choose_date")
 * @Template()
 */
public function chooseDateAction()
{
    return array( );
}

The reason for that I would like to give the route access to some users but the user role are different.

Let's say:

User with supervisor role can access sup_choose_date

User with user role can access user_choose_date

The question is if it is possible to have two different routes for one action? or I have duplicate the code for different routes ?

 Answers

1

Yes, it is possible when using YAML (or XML) routing.

Example:

sup_choose_date:
    pattern:   /supervisory/choose/date
    defaults:  { _controller: MyBundle:Default:chooseDate }

user_choose_date:
    pattern:   /edit/choose/date
    defaults:  { _controller: MyBundle:Default:chooseDate }
Friday, October 15, 2021
 
TV Nath
 
4

I would use a ParamConverter, yes. The default DoctrineParamConverter that ships with the FrameworkExtraBundle can handle most simple cases -- that is, it knows how to look up a typehinted object by a field with the same name as the route placeholder:

// routing.yml
foo_route:
    pattern: /{slug}/
    defaults: { _controller: FooVendorBundle:Foo:view }

// FooVendorBundle/Controller/FooController.php
public function view(FooEntity $foo)
{
    // $foo will be an instance of FooEntity
}

Normally, in a controller's argument list, you'd have a $slug variable that would be populated from the contents of {slug} captured by the route. However, with the ParamConverter, it recognizes that you're requesting a FooEntity class, and will try to find that entity by the captured slug value and populate the $foo variable with that entity.

The default ParamConverter is, of course, limited to only being able to look up properties that actually exist on the entity: if FooEntity does not have a field named slug, the lookup will fail and an exception will be thrown. Like I said, this will handle a majority of basic use cases. If you need more in-depth conversion of request parameters, you could always write your own.

Saturday, May 29, 2021
 
CMOS
 
4

Please try with

mysite.local/index.php/hello

If that works, that means either .htaccess file is missing or mod_rewrite is not enabled on apache server.

first you can run

composer require symfony/apache-pack

to add .htaccess automatically and test, if route works then everything is fine.

otherwise you've to edit httpd.conf or apache2.conf file and enable mod_rewrite

Saturday, May 29, 2021
 
Ticksy
 
3

C++17: Yes! You should use a structured binding declaration. The syntax has been supported in gcc and clang since gcc-7 and clang-4.0 (clang live example). This allows us to unpack a tuple like so:

for (auto [i, f, s] = std::tuple{1, 1.0, std::string{"ab"}}; i < N; ++i, f += 1.5) {
    // ...
}

The above will give you:

  • int i set to 1
  • double f set to 1.0
  • std::string s set to "ab"

Make sure to #include <tuple> for this kind of declaration.

You can specify the exact types inside the tuple by typing them all out as I have with the std::string, if you want to name a type. For example:

auto [vec, i32] = std::tuple{std::vector<int>{3, 4, 5}, std::int32_t{12}}

A specific application of this is iterating over a map, getting the key and value,

std::unordered_map<K, V> m = { /*...*/ };
for (auto& [key, value] : m) {
   // ...
}

See a live example here


C++14: You can do the same as C++11 (below) with the addition of type-based std::get. So instead of std::get<0>(t) in the below example, you can have std::get<int>(t).


C++11: std::make_pair allows you to do this, as well as std::make_tuple for more than two objects.

for (auto p = std::make_pair(5, std::string("Hello World")); p.first < 10; ++p.first) {
    std::cout << p.second << std::endl;
}

std::make_pair will return the two arguments in a std::pair. The elements can be accessed with .first and .second.

For more than two objects, you'll need to use a std::tuple

for (auto t = std::make_tuple(0, std::string("Hello world"), std::vector<int>{});
        std::get<0>(t) < 10;
        ++std::get<0>(t)) {
    std::cout << std::get<1>(t) << std::endl; // cout Hello world
    std::get<2>(t).push_back(std::get<0>(t)); // add counter value to the vector
}

std::make_tuple is a variadic template that will construct a tuple of any number of arguments (with some technical limitations of course). The elements can be accessed by index with std::get<INDEX>(tuple_object)

Within the for loop bodies you can easily alias the objects, though you still need to use .first or std::get for the for loop condition and update expression

for (auto t = std::make_tuple(0, std::string("Hello world"), std::vector<int>{});
        std::get<0>(t) < 10;
        ++std::get<0>(t)) {
    auto& i = std::get<0>(t);
    auto& s = std::get<1>(t);
    auto& v = std::get<2>(t);
    std::cout << s << std::endl; // cout Hello world
    v.push_back(i); // add counter value to the vector
}

C++98 and C++03 You can explicitly name the types of a std::pair. There is no standard way to generalize this to more than two types though:

for (std::pair<int, std::string> p(5, "Hello World"); p.first < 10; ++p.first) {
    std::cout << p.second << std::endl;
}
Tuesday, June 1, 2021
1

Since it is your custom filter, you can extend it to handle AllowAnonymous (if you don't want to use AllowAnonymous, yoy can create own f.e. NoAuthentication):

public class AuthenticateUser : IAuthenticationFilter
{

    public void OnAuthentication(AuthenticationContext filterContext)
    { 
        if (this.IsAnonymousAction(filterContext))
        {
            return;
        }

        // some code
    }

    private bool IsAnonymousAction(AuthenticationContext filterContext)
    {
        return  filterContext.ActionDescriptor
                             .GetCustomAttributes(inherit: true)
                             .OfType<AllowAnonymousAttribute>() 
                                             //or any attr. you want
                             .Any();
    }
}
Wednesday, December 22, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :