How to check the url in the controller

hi
i work with slim
and now i have a problem

i the a controllar

and 2 routs send to this controller

1)the page html with teig
2)the api and show that in json

now i need to check
if i comein with this url:
/product/{slug}

return the twig

if i comin to api/product/{slug}
reutn me the json

i do the json and the html return

i just to know how i check what url reqeut the that controller

If I’m understanding you correctly, what you are looking for is the route pattern. See the Current Route section of the docs for an example, but you can access the route pattern within your controller by looking in the ‘route’ attribute of the request object. So something like this depending on the variable name you assigned the request in your controller.

$route = $request->getAttribute('route');

Now look at pattern within $route

i have that:
Notice: Undefined variable: request in C:\wamp64\www**\app\Controller\Product.php on line 63

i have 2 routs and 1 controller
i need to know what send for the requst
one time need "html"
and second time need json

if i get that url:
name/product
i need to get from the controller the html page

if i go to url:
api/name/product
iget the json page

You will need to change $request to the variable name you assigned the request object in the arguments of your controller. For example in your controller you might have named it $req, or something else entirely. Alternatively, can you post your controller so we can take a look?

that is my controller
now i have 2 return
so i need a "if"
SOMTHING LIKE THIS:

if url == produc/{slug}
return HTML
else if url ==api/produc/{slug}
return json

public function getMain()
{
$slug = $this->args[‘slug’];

    $product = ProductModel::with('spec.fields', 'businessProducts', 'categories', 'tags', 'spec', 'specValues', 'group', 'manufacturer')->findBySlug($slug)->first();

    
    // json output
    //return $this->renderJson(compact('product'));

    return $this->render('product.main', compact('product'));
}

Where are you defining $this->args? Do you also have $this->req or $this->request? If so then you would use something like $route = $this->req->getAttribute('route') or $route = $this->request->getAttribute('route') to fetch the current route object.

dosnt work

u have a skype or somthing to help me plz/?

Can you post the entire route definition and the entire controller?

'product' => [
    'url'       => 'product/{slug}',
    'via'       => ['get', 'post'],
    'class'     => 'Product',
    'method'    => 'main',
    'before'    => ['checkProductExists'],
],
'api_product' => [
    'url'       => 'api/product/{slug}',
    'via'       => ['get', 'post'],
    'class'     => 'Product',
    'method'    => 'main',
    'before'    => ['checkProductExists'],
],

Okay, that is helpful. You might want to name the routes for convenience of referring to them later. Can you post your entire Product class (controller) as well?

 class Product extends Controller
{
  
 public function getMain()
    {
    $slug = $this->args['slug'];

        $product = ProductModel::with('spec.fields', 'businessProducts', 'categories', 'tags', 'spec', 'specValues', 'group', 'manufacturer')->findBySlug($slug)->first();

        
        // json output
        //return $this->renderJson(compact('product'));

        return $this->render('product.main', compact('product'));
    }

    }

Okay, so you have a base Controller where you must be defining $this->args = ... Can you either post that Controller as well, or does it have something like $this->req or $this->request?

ןi put in the comtorller:
echo $this->request;

andi get
Catchable fatal error: Object of class Slim\Http\Request could not be converted to string in C:\wamp64\www**\app\Controller\Product.php on line 62

I assume that $this->request is defined in the base Controller?

What about this:

$route = $this->request->getAttribute('route'); var_dump($route);

i get somthing:

sdsdsdsdsdsdssd

Okay, good. We now know where the Request Object is. :slight_smile:

So here is how I would do it. I would name each of your routes. See the docs for the section on route names. You might name your first route something like 'product.twig' and your second route as 'product.json' to identify each route.

Then in your controller you would do something like this.

$route = $this->request->getAttribute('route');
$routeName = $route->getName();

if ($routeName == 'product.twig') {
    // .. do output twig
}

if ($routeName == 'product.json') {
    // .. do output json
}

and what i need to put in my routes?

I’m not certain because you define your routes in a very different manner than I do, but probably something like this.

'product' => [
    'url'       => 'product/{slug}',
    'via'       => ['get', 'post'],
    'class'     => 'Product',
    'method'    => 'main',
    'name'      => 'product.twig',
    'before'    => ['checkProductExists'],
],
'api_product' => [
    'url'       => 'api/product/{slug}',
    'via'       => ['get', 'post'],
    'class'     => 'Product',
    'method'    => 'main',
    'name'      => 'product.json',
    'before'    => ['checkProductExists'],
],

However… you will need to figure out how your routes are defined and probably add in the name to the definition. But again, you are defining your routes very different from how I do so I’m not sure exactly how you would attach a name to your routes.

Were you able to figure it out? If you can show where your routes are being loaded from your route config I can probably help.

its work!!!
u the bast

many thanks

BAW
i do a big apps with the slim :slight_smile: