i've got the following url construction from my cms
domain.de/cmsfolder/page1/page2/function/my-news-headline
or
domain.de/cmsfolder/page1/function/my-reference-headline
i want to stripe out the function so that my url is
domain.de/cmsfolder/page1/page2/my-news-headline
or
domain.de/cmsfolder/page1/my-reference-headline
and didn't result in an 404 error.
can someone tell me if this is possible and how?
or perhaps it is possible to stripe out "function" out of any url of my page?
Best regards and thank you in advance
not an elegant solution, but this will work in case you only got 1 action ('myfunction') in your pages controller:
add this to Page_Controller:
private $actionName;
private static $allowed_actions = array (
'myfunction'
);
static $url_handlers = array(
'' => 'myfunction'
);
public function init(){
parent::init();
$urlParams = $this->getURLParams();
if(isset($urlParams['Action'])) { //might not be set when coming from '/' or '/home'
$this->actionName = $urlParams['Action'];
}
}
public function myfunction() {
//do something here with $this->actionName
}
now, when you navigate to domain.de/cmsfolder/page1/page2/my-news-headline, $actionName will be set to 'my-news-headline', and myfunction is called to work with it.
Related
I have a link I am sending via email. For example, www.swings.com/worker?id=3382&tok=jfli3uf
In this case I want the person to click the link, get sent to the login page(which it does) and then be directed to a controller method WITH the $id and $tok variables. I can't get that part to work. Any ideas? I am only using the RedirectIfAuthenticated class and this is what it looks like:
public function handle($request, Closure $next)
{
$user = $request->user();
if ($this->auth->check()) {
if($user && $user->hasRole('worker'))
{
return redirect('worker');
}
return redirect('home');
}
return $next($request);
}
hasRole is a method I created in the User model that checks the role of the logged in user
You can flash data to the session when redirecting by chaining the with() method:
// in your handle() method:
return redirect('home')->with($request->only('id', 'tok'));
// then in home controller method:
$id = session('id');
$tok = session('tok');
AFTER SOME HOURS I WAS ABLE TO HAVE A SOLUTION:
ReturnIfAuthenticated wasn't changed. I just added the following within my controller that this link should go to:
for instance, the route would be:
Route::get('worker', 'WorkerController#methodINeed');
Within this method:
public function methodINeed() {
$id = Input::get('id');
$tok = Input::get('tok');
// Do what I need this variables to do
}
What I didn't understand and what could not be properly understood is that the auth controller in Laravel 5 is triggered when a user is a guest it will still redirect to the actual method with all its original data once auth is successful. Hope this is helpful.
i have a url example.com/information/london. whenver some one calls this url i want to call a controller information and its index method. but i want to pass slug as jobs-in-london i.e. example.com/information/jobs-in-london how can i achive this by writing url rule in config/main.php.
i.e i want to redirect my page example.com/information/london to example.com/information/jobs-in-london but dont want to use .htaccess i want to achieve this only by url routing rules i have tried this by writing
'<_c:(information)>/<slug:london>'=>'information/index/jobs-in-london'
but this wont work for me.
class InformationController extends Controller
{
public function actionIndex($slug)
{
CVarDumper::dump($slug,10,true);
exit;
}
}
Your question is unclear. You may mean either of the following:
Url:
example.com/information/london
example.com/information/singapore
Rule:
'information/<slug>' => 'information/index',
Controller:
public function actionIndex($slug)
{
$slug = "jobs-in-".$slug;
var_dump($slug);
}
Slug Result:
jobs-in-london
jobs-in-singapore
Url:
example.com/information/jobs-in-london
example.com/information/jobs-in-singapore
Rule:
'information/<slug:jobs-in-.+>' => 'information/index',
Controller:
public function actionIndex($slug)
{
var_dump($slug);
}
Slug Result:
jobs-in-london
jobs-in-singapore
Here is what you will exactly have to do in the routes:
'<controller:(information)>/<slug:>' => '<controller>/index'
and here is how to add the slug while you're creating the URL:
Yii::app()->createUrl('information/index', array('slug' => 'jobs-in-london'));
and to check what the slug is, what you did to the function is correct.
I need to return a different view from a controller depending on the route that is requested.
For example: in my application I have clients, devices and campaigns. All have CRUD's created, but in some cases I want to do something like "view clients, delete his campaign and returning to the clients view" but my campaignsController#delete returns to campaigns by default.
The idea is not rewrite the same controller only changing the route of returning, does Laravel have something to help with this?
Thank you
Laravel will not control the whole flow of your application. If you have a campaign delete router:
Route::delete('campaign/{id}');
and it returns to campaigns
class CampaignController extends Controller {
public function delete($id)
{
$c = Campaign::find($id);
$c->delete();
return Redirect::route('campaigns');
}
}
You will have to trick your route to make it go to wherever you need to, there should be dozens of ways of doing that, this is a very simple one:
Route::delete('campaign/{id}/{returnRoute?}');
class CampaignController extends Controller {
public function delete($id, $returnRoute = null)
{
$returnRoute = $returnRoute ?: 'campaigns';
$c = Campaign::find($id);
$c->delete();
return Redirect::route($returnRoute);
}
}
And create the links in those pages by using the return route option:
link_to_route('campaign.delete', 'Delete Campaign', ['id' => $id, 'returnRoute' => 'clients'])
My module called Test, and a back-office controller to this is check, here is the code
class TestCheckModuleAdminController extends ModuleAdminController {
public function __construct()
{
echo "Checked!";
}
}
And when I'm going to mysite/admin/index.php?controller=check&module=test I'm getting the message that it doesn't exists, so whats went wrong?
I'm even turned on valid urls for it and it must be like this:
mysite/admin/module/test/check but no response to this.
Though if it is a Front Controller like this:
class TestCheckModuleFrontController extends ModuleFrontController {
public function __construct()
{
echo "Checked Front!";
}
}
url for this will be like mysite/module/test/check and it's allright.
I guess that it is no rewrite rules for this in .htaccess
I tried this...
RewriteCond %{HTTP_HOST} ^test.test$
RewriteRule ^admin([0-9]{4})/module/([a-z]+)/([a-z]+) module=$1&controller=$2 [L]
maybe it's not right, but close to the answer.
upd 1.
prestashop/modules/mymodule/controllers/admin/check.php
and it's source right now:
class TestCheckController extends ModuleAdminController {
public function __construct()
{
echo "Checked!";
}
}
What url I must to use for it?
upd 2. Well, I have a new tab in my admin/tabs list. But still can't go a controller on it.
What I can do with this tab btw? Maybe I can put it anywhere on admin menu isn't?
How can get this check controller from a url than?
For the name you should just write TestCheckController instead of TestCheckModuleAdminController. I did it recently and it works for me.
You can also extend the Prestashop menubar in the way you won't need to provide a token by yourself:
in you module install method, add the following code:
$parentTab = new Tab();
$parentTab->name[$this->context->language->id] = $this->l'('My module Top tab');
$parentTab->class_name = 'TopModuleNav';
$parentTab->id_parent = 0;
$parentTab->module = $this->name;
$parentTab->add();
$adminMenuItem = new Tab();
$adminMenuItem->name[$this->context->language->id] = $this->l'('Admin Menu Item');
$adminMenuItem->class_name = "TestCheck";
$adminMenuItem->module = $this->name;
$adminMenuItem->id_parent = $parentTab->id;
$adminMenuItem->add();
I hope it helps!
At BackOffice your controller is "TestCheckModule"
so you should call it via:
admin/index.php?controller=TestCheckModule&token=xxxxxxxxxxxxxxxxxxxxxxxxxxx
you should provide a proper token as well.
You do not need to provide the module name in the URL.
It's a good practice to prefix the controller with "Admin"
I have a resource in Laravel I have called artists with an ArtistsController. I would like to add filters to some of the pages, but not all. I know I can add a filter to all of the functions/views in the resource controller like so:
public function __construct()
{
$this->beforeFilter('auth', array('except' => array()));
}
How do I add the beforeAuth filter to only a certain view/function? I would like a user to be logged in in order to go the "index" view, but I would like a user to be able to go to the "show" pages without necessarily being logged in:
public function index()
{
$artists = Artist::all();
return View::make('artists.index', compact('artists'))
->with('artists', Artist::all())
->with('artists_new', Artist::artists_new());
}
public function show($id)
{
$artist = Artist::find($id);
return View::make('artists.show', compact('artist'))
->with('fans', Fan::all());
}
Is there a way to do this? Thank you.
Not sure if this helps but you could use the only key instead of the except (if I understand your question correctly).
$this->beforeFilter('auth', array('only' => array('login', 'foo', 'bar')));
Although that would still go in the constructor.