yidas/codeigniter-rest

Add response header?

zkelo opened this issue · 6 comments

zkelo commented

Hi!

I need to add a header Access-Control-Allow-Origin in response of API. How I can do that?

yidas commented

Hi @zkelo,

You can add it with output set_header() before return response in a API method:

class ApiController extends yidas\rest\Controller
{
    public function index()
    {
        $this->output->set_header('Access-Control-Allow-Origin: *');

        return $this->response->json(['bar'=>'foo']);
    }
}

Cheers.

yidas commented

You are mentioning a good point that I will add the header interface into Response component.

yidas commented

Hi @zkelo,

Header feature is release from v1.6.1:

https://github.com/yidas/codeigniter-rest#withaddedheader

Cheers

zkelo commented

Hi @zkelo,

Header feature is release from v1.6.1:

https://github.com/yidas/codeigniter-rest#withaddedheader

Cheers

Thanks!

zkelo commented

But what if I need to send some headers for every response? Can you add a config file for your controller for adding response headers by default?

yidas commented

@zkelo

You could use constructor to do it:

class ApiController extends yidas\rest\Controller
{
    function __construct()
    {
        $this->response->withAddedHeader('Access-Control-Allow-Origin', '*');
    }
}