In cakePHP 4
I have a controller and view.php connected with it.
I can use a route like this: sitename.com/projects/45, where 45 - is sample project ID.
Using this url I can reach a page with the content of particular project. But If I want to construct something like a page of settings of this project, how I have to do it?
For example via url sitename.com/projects/45/settings
Help please
It's simple:
// sitename.com/projects/45
// public function view($id) { ... }
// sitename.com/projects/45/settings
public function view($id, $passed = null) {
if($passed == 'settings') {
// do ...
}
}
or
public function view($id) {
$passed = $this->getRequest()->getParam('pass');
if (in_array('settings', $passed)) {
// do ...
}
}
Related
I have a problem with routing on my website as it's not working at all. At the beginning I wanted to have different Controllers for differetn tabs, but in the end I have changed that to view pages from the Home controller, but even that doesnt work.
Here is my Home controller - index and connection to Model works fine but the travel() function doesnt
<?php
namespace App\Controllers;
use App\Models\HomeMod;
use \Codeigniter\Controller;
class Home extends BaseController {
public function index() {
$this->homeMod = new HomeMod();
$data = array();
$data['total'] = number_format($this->totalCases('total'));
echo view('header');
echo view('welcome_message', $data);
echo view('footer');
}
protected function totalCases(String $columnName) : String{
$result = $this->homeMod->findColumn($columnName);
return count($result) > 0 ? $result[0] : "0";
}
public function travel() {
return view('travel');
}
public function getPostCodeCases() {
if(!IS_AJAX) return;
var_dump("inside");
die();
}
}
I cannot reach the getPostCodeCases() function either (the ajax call in js file works fine, its just not reaching this function).
And my routes file - as you can see i have tries with different variations
$routes->get('/', 'Home::index');
// $routes->get('/getPostCodeCases', 'Home::getPostCodeCases');
$routes->post('/getPostCodeCases', 'Home::getPostCodeCases');
$routes->match(['get', 'post'],'/getPostCodeCases', 'Home::getPostCodeCases');
$routes->get("travel", "Travel::index");
// $routes->add('/travel', 'Travel::index');
// $routes->add('travel', 'Home::travel');
// $routes->add('/travel/(:any)', 'Travel::index');
My base url: https://localhost/CI and $indexPage = '' (in App.php).
I am sure I have missed something, somewhere but not sure where...
Could someone help me to resolve this problem ?
Sorry for my English, but what I'm trying to say is explained below.
I have a controller say ControllerCard which has an action like this.
function actionScanCard()
{
...
$this->redirect('/transaction/redeem');
...
}
In other controllers, ControllerTransaction, I am trying to get that it comes/redirected from /card/scan-card
function actionRedeem()
{
$redirectFrom = ????;
if ($redirectFrom === '/card/scan-card')
{
// some actions
}
else
throw new ForbiddenHttpException('Must scan card!');
}
How do I get this $redirectFrom value with Yii2?
You could use the remember() & previous() methods in yii\helpers\BaseUrl.
function actionScanCard()
{
...
\yii\helpers\Url::remember();
$this->redirect('/transaction/redeem');
...
}
in TransactionController (or other)
function actionRedeem()
{
$url = \yii\helpers\Url::previous();
if($url === Url::to('card/scan-card')) {
// some actions
} else{}
}
I'm trying to achieve a thank you page after submitting a form in Zend 1.12
in the index i have form and i want if the validation passes then should go to another view (NOT INDEX) for thank you page. how can i do this in my code:
public function indexAction()
{
// action body
$C_form = new Application_Form_Eform();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($C_form->isValid($formData)) {
$this->_helper->redirector('','result');
exit;
} else {
$C_form->populate($formData);
}
}
$this->view->form = C_eform;
}
and after that where should i create the .phtml file? in the application\views\scripts\index?
I think you're looking for render:
$this->view->render('index/yourotherview.phtml');
In this case, index/ is referring to your views/scripts/index folder, and the yourotherview.phtml file.
So, all together it would be:
public function indexAction()
{
// action body
$C_form = new Application_Form_Eform();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($C_form->isValid($formData)) {
$this->_redirect('/index/result);
} else {
$C_form->populate($formData);
}
}
$this->view->form = C_eform;
}
EDIT:
From your comment it looks like you just want to be redirected instead of displaying a different view. In this case, it's as easy as creating a new action and making the view for it:
public function resultAction() {
// Code here if you need it
}
Then create the file result.phtml in the index/ views directory, and you'll need $this->_redirect('/index/result'); in the index controller. (See above code)
I am trying to take advantage of the Custom URL Rule Classes in Yii r1.8
I am trying to take something that looks like orgs/view/id/24 and instead display the name of the org as identified by Name in the db (i.e. changing www.mysite.com/orgs/view/id/24 to www.mysite.com/jaysshop dynamically ). Unfortunately I am not getting it to work.
Here is my code:
class OrgsUrlRule extends CBaseUrlRule
{
public $connectionID = 'db';
public function createUrl($manager,$route,$params,$ampersand)
{
if ($route==='orgs/view/id') //even tried 'orgs/view' or 'orgs/index'
{
if (isset($params['Name']))
return $params['Name'];
else if (isset($params['Name']))
return $params['Name'];
}
return false;
}
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches))
{
}
return false;
}
}
urlManager:
array(
'class' => 'application.components.OrgsUrlRule',
'connectionID' => 'db',
),
I'm giving you this example in the assumption that you want to take the name of a shop from the URL trigger the your controller/action on it
For example:
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
$aParts = explode('/', $pathInfo);
if (count($aParts) == 1) // It's only 1 piece, so a possible "shop name"
{
if (isAValidNameForAShop($aParts[0]))
{
$_REQUEST['id'] = $aParts[0]; // Store it to retrieve it in the controller
return 'orgs/view';
}
}
return FALSE; // Seems like something else, we don't apply
}
Hope that helps you along a bit.
Im using CodeIgniter to write a site ... I understand $_GET requests are now used like so www.website.com/function/value .. and in the controller getting a url segment is written like so:
$userId = $this->uri->segment(3, 0);
Im just wondering, when a controller loads, i want to check if there is any uri segments, if there is then push to one view, else if there isnt a uri segment push to another.
Is that possible?
cheers.
You can use your controller arguments for that too.
When accessing /user/profile/1 your controller named User will call the method profile() and pass the number 1 as the first argument to your method. Like so:
class User extends CI_Controller {
{
public function index()
{
$this->load->view("user_index");
}
public function profile ( $userId = null )
{
if( (int)$userId > 0 )
$this->load->view("user_profile");
else
$this->load->view("another_view");
}
}
This is a very basic sample and I'm just trying to show the idea.
Seems like your asking two questions...
First, to check if the request is get
public function get_test()
{
if($_SERVER['REQUEST_METHOD'] == "GET")
{
//do something from get
echo "GET";
}
else
{
//do something not get
echo "NOT GET";
}
}
The next question seemed to be checking uri segments
public function get_test()
{
if($_SERVER['REQUEST_METHOD'] == "GET")
{
//do something from get
//echo "GET";
if($this->uri->segment(3)) //is true as is not empty
{
echo $this->uri->segment(3);
}
else
{
echo "I am nothing without my URI Segment";
}
}
else
{
//do something not get
echo "NOT GET";
}
}
As I understand you can use PHP default value.
function myFunction($var1 = NULL) {... if($var1 === NULL) ...}
Now if you do not pass the param you will get the NULL value.
I am still not using version 2 of codeigniter but this framework do not accept get requests; unless you mess with the configuration. Theres a function $this->input->get('myGet') you should look around at de the codeigniter.com/user_guide