How to get full path URL in phalconphp - phalcon

I'm new to phalcon and I need assistance because i cannot find a way to get full path url (and add it in an email message) such as :
"http://phalcon.mydomain.com/auth/confirmRegistration"
the most i could get is "/auth/confirmRegistration"
using $this->url->getStatic('auth/confirmRegistration')
there might be a build in function but i could not discover it, or maybe i should put the 'http://phalcon.mydomain.com' part in a global variable ??
thank you in advance

Here's a simple way to get the url of the current page in Phalcon:
echo $this->router->getRewriteUri();

You could do two things. Per documentation you could set the full domain url:
<?php
$url = new Phalcon\Mvc\Url();
//Setting a relative base URI
$url->setBaseUri('/invo/');
//Setting a full domain as base URI
$url->setBaseUri('//my.domain.com/');
//Setting a full domain as base URI
$url->setBaseUri('http://my.domain.com/my-app/');
Or you could simply use HTTP_HOST / SERVER_NAME to get the host name and append the rest of it, e.g:
echo 'http://' . $_SERVER['SERVER_NAME'] . '/auth/confirmRegistration';

Related

F3: Rerouting to a dynamic URL

I am having some difficulties rerouting to a dynamic URL from within my controller.
in routes.ini
GET /admin/profiles/patient/#patientId/insert-report = Admin->createReport
in the controller Admin.php, in method createReport():
$patientId = $f3->get('PARAMS.patientId');
My attempt (in Admin.php):
$f3->reroute('admin/profiles/patient/' . echo (string)$patientId . '/insert-report');
Question: How to reroute to the same URL (where some error messages will be displayed) without
changing completely the routing, that is attaching patientId as a URL query parameter ?
Thanks, K.
The echo statement is not needed to concatenate strings:
$f3->reroute('admin/profiles/patient/' . $patientId . '/insert-report');
Here are 3 other ways to get the same result:
1) build the URL from the current pattern
(useful for rerouting to the same route with a different parameter)
// controller
$url=$f3->build($f3->PATTERN,['patientId'=>$patientId]);
$f3->reroute($url);
2) reroute to the same pattern, same parameters
(useful for rerouting from POST/PUT/DELETE to GET of the same URL)
// controller
$f3->reroute();
3) build the URL from a named route
(useful for rerouting to a different route)
;config file
GET #create_report: /admin/profiles/patient/#patientId/insert-report = Admin->createReport
// controller
$url=$f3->alias('create_report',['patientId'=>$patientId']);
$f3->reroute($url);
or shorthand syntax:
// controller
$f3->reroute(['create_report',['patientId'=>$patientId']]);

How to rewrite rules NginX in this example?

I am creating an API to feed some apps.
So the app could call these possible URLs to get information from the database;
mysite.com/api/v1/get/menus/list_tblname1.json.php
mysite.com/api/v1/get/menus/list_tblname1.json.php?type=arr
mysite.com/api/v1/get/menus/list_tblname2.json.php
mysite.com/api/v1/get/menus/list_tblname2.json.php?type=arr
In php I have already the code that grabs the tblname from the URL and give me back all the table content. It works good (it is not the final version).
But now I find myself copying and pasting the same code for each page where the URL points to. Here is the code:
<?php
header('Content-Type:application/json');
include_once '../../../../class/db.php';
$verb=$_SERVER['REQUEST_METHOD'];
$filePath=$_SERVER['SCRIPT_NAME'];
$split1 = explode("/", $filePath);
preg_match("/(?<=_)[^.]+/", $split1[5], $matches);
$tableName = $matches[0];
if ($verb=="GET") {
header("HTTP/1.1 200 ok");
if(isset($_GET['type']) && $_GET['type']=="arr"){
echo db::get_list($tableName,'arr');//Reply ARRAY
}
else{
echo db::get_list($tableName);//Reply JSON
}
}
else{
die("Nothing for you at this page!");
}
I mean, I have the same code inside each these pages.
list_tblname1.json.php
list_tblname2.json.php
I am not sure how to solve this situation but I think that this is case for
rewrite rules.
So, I think a possible solution is to create one page that could call
returncontent.php for example and create rules in the server that should point to the same page when certanlly pages are requested and pass the parameter $tableName to the page. I think I should pass the regex to my server and grab the $tableName with $_GET[] (I think) inside returncontent.php.
I am not sure about it.
I am using NginX.
How to implement it in this scenario?
As a rule, it's bad practice to parse a URI in NginX and pass the result downstream.
Rather: mysite.com/api/v1/get/menus/returncontent.php?file=list_tblname2.json
No changes to NginX needed. Parse the query param (file) in PHP.

How to make seo url for Yii $_GET method using url manager?

I'm working on a site on local server. I have made a form to search country,state and city. After getting the results I see the URL formatted as URL
I want to make this URL as URL
So here I want to know about URL manager rules so I can make it as I want.
Simply add this rule in your url-manager
"site/searchme/<country>/<state>/<city>" => "site/searchme"
Now, you need to have an action with this signature:
public function actionSearchme($country, $state, $city)
You can access $country, $state, $city from url inside this action. For example if your url be like http://localhost/yii_1/site/searchme/UnitedStates/Washington/NewYork, $country will equal "UnitedStates" and so on.

Yii transform url from param/param/param/ to param/param?param

Suppose I have a url like:
site.com/param1/value1/param2/value2/param3/value3/param4/value4
I need to convert this url when a user writes it in url line to:
site.com/param1/value1/param2/value2?param3=value3&param4=value4
P.S. - the number of parameters is variable.
How can I do it?
You need to change the UrlManager Rules according to the situation.
You need to configure the Url manager to handle the first two params as path url and let the rest

Error 400 Your request is invalid in yii if i am creating link

I'm pretty new to Yii, so this might be a silly question. I must be missing something somewhere. Plz help me out.
I have just written a simple code while I'm learning Yii.
I have a spark controller which has an action that looks like this:
public function actionDownload($name){
$filecontent=file_get_contents('images/spark/'.$name);
header("Content-Type: text/plain");
header("Content-disposition: attachment; filename=$name");
header("Pragma: no-cache");
echo $filecontent;
exit;
}
Now I have enabled SEO friendly URLs and echo statement in the view file to display the returned download file name.
But when I go to the URL,
SITE_NAME/index.php/spark/download/db5250efc9a9684ceaa25cacedef81cd.pdf
I get error 400 - your request is invalid.
Plz let me know if I am missing something here.
Yii keeps an eye on the params you pass into an action function. Any params you pass must match what's in $_GET.
So if you're passing $name then you need to make sure there's $_GET['name'] available.
So check your URL manager in /config/main.php, and make sure you're declaring a GET var:
'spark/download/<name:[a-z0-9]+>' => 'site/download', // Setting up $_GET['name']
Otherwise change your function to the correct param variable:
public function actionDownload($anotherName){
}
Did you create the URL correct? You should do it best with
Yii::app()->createUrl('/spark/download',array('name'=>$filename));
Make sure if you are properly supplying the parameter name ($name) in your request.
Use
SITE_NAME/index.php/spark/download/name/db5250efc9a9684ceaa25cacedef81cd.pdf
or
SITE_NAME/index.php/spark/download?name=db5250efc9a9684ceaa25cacedef81cd.pdf
instead of just SITE_NAME/index.php/spark/download/db5250efc9a9684ceaa25cacedef81cd.pdf