The currentRouteName method in PHP retrieves the name of the current route that the application is using. This method is commonly used in web development frameworks to help identify the route that a user is currently accessing.
Example 1: Laravel Framework In Laravel, the currentRouteName method can be accessed from the request object. Here is an example code snippet that shows how it can be used:
use Illuminate\Http\Request; use Illuminate\Support\Facades\Route;
Route::get('/', function (Request $request) { $routeName = $request->route()->getName(); if ($routeName == 'home') { echo 'Hello from home page!'; } });
In this example, the request route's name is retrieved using the currentRouteName() method and then compared against the name of the home route. If the names match, a message is printed on the page.
Example 2: Symfony Framework In Symfony, the current route name can be obtained from the request object as well. Here is an example code snippet that shows how it can be used:
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\HttpFoundation\Request;
class MyController extends AbstractController { /** * @Route("/products/{id}", name="product_show") */ public function showProduct(Request $request) { $routeName = $request->attributes->get('_route'); if ($routeName == 'product_show') { echo 'Here is the product page!'; } } }
In this example, the route's name is retrieved from the request's attribute object using the "get" method. The retrieved name is then compared against the product_show route's name.
Package/library: For Laravel Framework, this belongs to the Illuminate HTTP package/library. For Symfony Framework, this belongs to the Symfony HTTP Foundation package/library.
PHP Route::currentRouteName - 30 examples found. These are the top rated real world PHP examples of Route::currentRouteName from package someline-starter extracted from open source projects. You can rate examples to help us improve the quality of examples.