Linking to the new Joomla 2.5 Controller - joomla

As many of you know the controller in Joomla 2.5 changed from
// Create the controller
$classname = 'mycomponentController'.$controller;
$controller = new $classname( );
// Perform the Request task
$controller->execute( JRequest::getVar('task'));
// Redirect if set by the controller
$controller->redirect();
to something along the lines of
// Get an instance of the controller prefixed by the component
$controller = JController::getInstance('mycomponent');
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
// Redirect if set by the controller
$controller->redirect();
Now in Joomla 1.5 as well as by using the table you could run a task by running the link
index.php?option=com_mycomponent&controller=specificcontroller&task=randomtask
However this style of link doesn't work with the new controller - does anyone know how to format this link in Joomla 2.5 if you're using the new controller?

You can combine task and controller so that it'll call the task of the specified controller.These will be .(dot) seperated. try this-
index.php?index.php?option=com_mycomponent&view=viewname&task=specificcontroller.randomtask
Read More - http://docs.joomla.org/JController_and_its_subclass_usage_overview

Related

Why Laravel method call twice when using route()->redirect with parameters & code 301?

Please help me, I don't know why Laravel method is called twice times when I use command this command to redirect to new page:
Route:
Route::post('/editor/create/{productCode}', 'EditorController#create')->name('create-new-design');
Route::get('/editor/{designCode}', 'EditorController#edit')->name('edit-design');
EditorController:
public function create($productCode) {
// .. do some thing & redirect to editor page
return redirect()->route('edit-design', ['designCode' => $newDesignCode], 301);
}
public function edit($designCode){
// this method is called twice
$design = Design::where('code', '=', $designCode)->first();
// do extra options --> return editor edit view
return view('editor.edit');
}
Flow:
User request to create new Design by call action [POST]: /editor/create/{productCode} --> Server process & create Design Record then redirect user to editor page ( --> /editor/{designCode} ).
Question:
Why function public function edit($designCode) is call twice when user is redirected to edit page (or reload this page after create new design ) ?
Notes:
This project, I'm using:
Apache server
Laravel 5.8.*
Thank you,
try this
return redirect()->route('edit-design', ['designCode' => $newDesignCode]);
No Need To Pass the 301 again. I believe that might be the cause. Or You can share your edit method. It might contain policies/permission look ups that is causing the double execution.
Try like this:
return redirect()->route('edit-design', $newDesignCode);
After each HTTP request, the page needs a refresh. It's the standard procedure. If you don't want to refresh the page, you can use AJAX calls and manage events with Javascript.

Laravel simple program

I am new to Laravel.
../laraone/public/ is working but cannot go further to create a simple aboutus page without the use of controller.
What is the pattern of URL to be used to get aboutus page.
Laravel uses MVC architecture. So to create a page you must go through A Controller and View (Model if you want to use DB and do data processing).
Soltuion # 1
You have to create a route first (Assuming Laravel 5.2):
// app/Http/routes.php in later version there is a separate folder for routes.
project_root/routes/web.php
Route::get('about-us','HomeController#aboutUs');
Controller :
// app/Http/Controllers/HomeController.php
public function aboutUs(){
return view('pages.aboutus'); //this will look for views/pages/aboutus.blade.php
}
View:
project_root/views/pages/aboutus.blade.php //put you html in this file
Solution # 2
Route:
// app/Http/routes.php in later version there is a separate folder for routes.
project_root/routes/web.php
Route::get('about-us', function () {
return view('pages.aboutus'); //this will look for views/pages/aboutus.blade.php
});
View:
project_root/views/pages/aboutus.blade.php //put you html in this file
Solution # 2 is not recommended but does the job. If you are new to laravel and learning it go for Solution # 1

configuring dynamic url's in router

I'm using Codeigniter. Basically what I want is to remove the Controller name (Home) from the url.
Those urls look like:
http://localhost/mechanicly/Home
http://localhost/mechanicly/Home/about
http://localhost/mechanicly/Home/contactus
now there are two ways I can remove the Home controller:
static definition of every url inside route:
$route['about'] = "Home/about";
$route['contactus'] = "Home/contactus";
I can use call backs in routes in Codeigniter:
$route['(.+)'] = function ( $param ) {
if( strpos( $param, "Admin" ) !== false ) {
echo $param;
return "Admin/".$param;
}
else{
echo $param;
return "Home/".$param;
}
};
this logic is much better as it is generic and I don't have to create new routes every time for new method inside the controller.
It is working fine for the client controller which is Home but I have another controller named as Admin and I want to redirect Admin requests to the Admin controller and Home request to the Home Controller.
Why does above code work fine for the Home controller but returns
not found
when I validate for the Admin controller?
I am using CI version 3.x
If you really want to get crazy, you could parse the methods from the controller file and programatically create the "static" approach.
Pseudo code here
$controller_file_contents = file_get_contents('path/to/controller.php');
$controller_methods = function_that_parses_methods_from_file($controller_file_contents);
foreach ($controller_methods as $controller_method) {
$route[$controller_method] = "Home/" . $controller_method;
}
How function_that_parses_methods_from_file works is probably gonna involve a regex, something like function \w+. If you go with this approach, try to keep the controller as small as possible by offloading as much logic as possible into models, which is often a good idea anyways. That way the performance impact in the router is as small as possible.
Alternatively, you may be able to parse the controller using get_class_methods if you can figure out how to load the controller into memory inside the router without conflicting when you need to load the controller using the router or causing too much performance issues.
Pretty goofy, but every method you create in that controller will automatically create a route.
you can create your menu(urlĀ“s) from db like
tbl_menu tbl_level
---------- -------------
id id
fk_level level
name dateUP
dateUP active
active
In your controllers you need to call the correct menu by session or wherever you want
then you can has this in your route.php
$route['(.+)'] = 'int_rout/routing/' . json_encode($1);
in your controller Int_rout.php
public function routing ( $param ) {
$routing = json_decode($param);
$routing = explode('/', $routing);
//$menu -> get menu from model
foreach($menu as $item){
if($routing[0] === $item->name){
//$level -> get level from model
$redirect = $level->level;
}
}
//the final redirect will be like
//admin/user or admin/user/12
//public/us
$params = ( empty($routing[1])) ? '' : '/' . $routing[1];
redirect($redirect . '/' . $routing[0] . $params, 'refresh');
}

Dynamic Controllers in CodeIgniter

I am in the process of creating a new website which loads all master and child categories from the database. I have tested the navigation as well, i.e., if I click any master category, it perfectly loads all the respective child categories without any issue. However, at present, I am doing this by passing query string in the URL. For instance
http://localhost/MyController?id=32145
Let's assume that the id, 32145, represents a master category namely 'About us'. My question is how can I change the above URL to something like:
http://localhost/Aboutus
and if there is any child category under About us than it should display as:
http://localhost/Aboutus/Mission
Please help me out as I am really stuck with this problem.
by default CodeIgniter uses a segment-based approach, you can do URL routing in way like your second part of the question - "and if there is any child category under About us"
$route['product/(:any)'] = "catalog/product_lookup";
more here: https://ellislab.com/codeigniter/user-guide/general/routing.html
but if you want to rewrite complete URL than you should probably check .htaccess rewriting
It is not easy, do once for migration.
In database you can store the New controller/url for products (if it is not have yet)
Create new Controllers
Route controller which redirect the old Url to the New Url
controllers
Route old urls to Route controller
Route controller something like this:
public function old_url($aProdId) {
if (is_null($aProdId)) {
// error cannot be null
}
$NewUrl = $this->new_url_model->getNewUrl($aProdId);
if (!$NewUrl) {
// error new url not exist
return;
}
redirect(base_url($NewUrl), 'refresh');
}

File Uploader in Joomla

I've been following the documentation here to add a (improved) file upload section to an existing component.
The link in the example above to the controller/model to then process the upload is formed through the post params:
post_params:
{
"option" : "com_mycomponent",
"controller" : "mycontroller",
"task" : "mytask",
"id" : "'.$myItemObject->id.'",
"'.$session->getName().'" : "'.$session->getId().'",
"format" : "raw"
},
My problem is the upload isn't working using the new controller methods introduced in Joomla 2.5:
// Get an instance of the controller prefixed by the component
$controller = JController::getInstance('mycomponent');
// Perform the Request task
$controller->execute(JRequest::getCmd('task'));
// Redirect if set by the controller
$controller->redirect();
This worked (and indeed on Joomla 2.5) does work absolutely fine on the old 1.5 method for loading a controller:
// Create the controller
$classname = 'mycomponentController'.$controller;
$controller = new $classname( );
// Perform the Request task
$controller->execute( JRequest::getVar('task'));
// Redirect if set by the controller
$controller->redirect();
Whilst this latter method is Joomla 2.5 compatible, unfortunately the component I wish to integrate this with uses the newer method and I'd rather not change this so I can keep updating the component as required without having to change this every time. Also if I did change it I'm guessing I may loose the existing features.
Basically I want to know how to set up the post params so that the new controller method is called correctly!
EDIT
I have since tried using a post param configuration of:
post_params:
{
"option" : "com_mycomponent",
"task" : "mycontroller.mytask",
"id" : "'.$myItemObject->id.'",
"'.$session->getName().'" : "'.$session->getId().'",
"format" : "raw"
},
I an attempt to emulate a link along the lines of index.php?option=com_mycomponent&task=mycontroller.mytask etc. But this still doesn't work either
You need to define below variable in index.php
define('_JREQUEST_NO_CLEAN', 1);
I was looking for the cause and I found this-
http://docs.joomla.org/Framework_Compatibility
Note- If this does not work remove "format" : "raw".
Let me know if it does not work.

Resources