So - I'm new to Laravel and we're using version 3.
I have a home page setup & working - say http://dev.mywebsite.com
Now I want to click a link on the page and redirect to a nice SEO friendly URL, but pass in the variables. In straight PHP this is simple - I can make http://dev.mywebsite.com/vacancies/town/page2 rewrite as http://dev.mywebsite.com/?where=town&page=2
But I can't get this to work in Laravel.
I know the full answer is to create controllers & views, but I have all the logic for the display in an included file so don't really want to change... is there any way to do this using either routes or mod-rewrite?
Thanks
Although I strongly recommend you to make a view with that file, you can still use PHP's Output Control to avoid messing with Laravel's rendering flow:
Route::get('your-nice-url-here', function() {
ob_start();
include 'your-raw-php-here';
return Response::make(ob_get_clean());
});
Related
Hey friends i am switching from core php to laravel framework and every thing works fine but the main problem is that my old version of site is based on this url pattern
https://m.apkleet.com/apk.php?app=garena-contra-returns
And my new laravel project url is something like this
https://m.apkleet.com/apk/garena-contra-returns
And if I change my site to laravel then it effects my seo due to this different url patterns...
my question is that how to redirect ugly url to laravel url.... Is there anything present in laravel to do this
Any help or recommendation will highly appreciated...
On your route define, just set your URL to apk.php. Something like this
Route::get('apk.php', [YourControllerClass, 'index'])->name('routeName');
And to generate the url for HTML, you need to pass app in param
route('routeName', ["app" => "garena-contra-returns"])
here is the anser of my question
Route::get('apk.php', function () {
$apk = $_GET['app'];
return redirect('apk/'.$apk);
});
Is there any way in the CI routing file I can find out if a view is using a mobile?
and if so rotate them to a different page?
I've got two sets of controllers one for mobile and one for desktop but I'd like the urls to always be the same.
I've tried adding the following code to the routes config file but I'm getting an error...
Am I thinking about this totally wrong?
$this->load->library('user_agent');
if ($this->agent->is_browser())
{
$route['default_controller'] = "index";
} elseif ($this->agent->is_mobile())
{
$route['default_controller'] = "m/index";
}
The error I'm getting is Undefined property: CI_Router::$load
From examining the CI system files, it appears that the Loader class is loaded after the Router class, so $this->load doesn't exist yet.
Check out CI Hooks, though: https://ellislab.com/codeigniter/user-guide/general/hooks.html
In addition, you might try using head.js (http://headjs.com/) and defining screen widths for Responsive Design. It enables you to build a web site and change the CSS to change the page depending on the width of the browser. Unless you are aiming at mobile/desktop due to functionality, screen size is the common reason we care what they are on, right? So, if it is screen size, I think the head.js system is the way to go. Then you have only one codebase of server-side stuff to worry about.
I was wandering which solution is better to use in Yii framework,
1) Redirecting page from somethingController.php
$this->redirect(array($this->id."/something"));
|| or
2) Creating Url
$this->createUrl($this->id."/something");
in view using contoroller & action you need.
Or maybe there is a better solution?
Thanks
It like ask what is better miles or pounds?. That functions are very different.
You need to use redirect when need to change page without user action in some conditions, for example in controller:
if($money==0)
{
$this->redirect(array('alerts/notEnoughMoney'));
}
If you want to generate address what will used for example in html links, then you need to use createUrl, because it will:
Avoid unnecessary step with redirect
Better for SEO, and will be more user friendly
Better for customizing
You can use createUrl in view, for example:
<?php
$link = $this->createUrl(array('user/profile'));
?>
My Profile
In any case, if you using redirects what visible for search bots you need to add second parameter:
$this->redirect(array('alerts/notEnoughMoney'),301);
----------------------------------------------^^^^
With this parameter bot will understand what this next page is permanent and will cache it as "main".
$this->createUrl($this->id."/".$this->action->id);
is the better because it will work with URL manager also and give rewrite urls.
I'm new to the world of API programming, I just have a bit of a side project at work at the moment and I'm learning as I write, so bear with me.
I'm unsure as to the best way to implement an API for multiple different functions. At the moment I just have a test script I run and an apache redirect that redirects anything under /api to this script, call it TestAPI.php (so /api/anything will redirect). I pass the path variable of the API to the script (so in that example the path would be 'anything').
At the moment I'm just writing it for 1 purpose, to look up some data based on the path, and eventually be about to update and delete etc with PUT/DELETE etc (it's restISH not restFUL). This is fine at the moment where everything redirects to this script, but what about if I need 2 different functions? So I want to look up a different data set? So for example now /api/data1 would go to the 1st set and /api/data2 would go to the second. This is where I start to get unsure.
Do I simply have 1 mega script that grows and grows so that /api/data1 and /api/data2 redirect to the same place (and thus handle any errors like 404s there). Or do I have a script for /api/data1 and /api/data2 with separate redirects to each, then a generic catchall for 404s (I would always like to return JSON/XML rather than HTML for a 404, so I need at least logic to return based on the Accept header).
As a 3rd option, do I have some sort of frontline controller that catches everything, then calls off to the sub components? So 1 script that is redirects to for anything under /api, which then calls off to the required components or 404s if it's an invalid path. This seems like the best way to do it to me, but I have no idea how. Do I have some section of the site that only that script can call, or do I use cURL from the frontline controller to the back end API sections (as I'd need to pass POST/PUT data I assume I'd have to use cURL, is there any other way?). How is this best implemented in Apache?
Yes, you use a front controller. The front controller can use convention like first thing after /api processes the request
i.e.
/api/firstprocessor/method1
/api/firstprocessor/method2
/api/secondprocessor/method14
You can check out Zend_Framework for an example of this in action, or it can be something as simple as
$name = 'Script_' . $this->generateCommandName($request->getPathVariable(1));
$this->executeScript($name, $request);
public function executeScript($class, Request $request) {
if (file_exists("scripts/".$class.'.php')) {
//include the script
require_once "scripts/".$class.'.php';
//execute the script
$command = new $class;
$command->execute($request);
}
}
Then all your scripts just have an execute method that uses $request to get $_GET or $_POST variables
I'm not able to figure this out on my own so here I am asking for your help.
How do I load a website that I already made as a view in the code igniter default controller?
I put my website under a folder name site, and in the default controller I loaded the view site/index , but then in my site there are problems with the includes and redirects... I don't know why, I guess the way the site usually works with redirecting isn't compatible with code igniter style
edit: I guess I would have to turn off CI engine for this site, but I don't know why, because I would still need codeingiter to manage other parts of my application
"CodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes.php file and set this variable:
$route['default_controller'] = 'Blog';
Where Blog is the name of the controller class you want used. If you now load your main index.php file without specifying any URI segments you'll see your Hello World message by default."
http://codeigniter.com/user_guide/general/controllers.html
fragment copied from that link , you should put the controllers classname in that config, not the view
I guess it's better to choose one of these options:
Modify the existing site to a CodeIgniter site.
Keep your site separate from the CodeIgniter site, and just link between the two sites.
The way you are trying to do it seems very useless and causing a lot of extra trouble.
You can simply use the redirect function in your controller. If you supply a full URL you can go to any other page. You will, of course, leave your CI app.
redirect('http://www.example.net/page_in_external_site/');
Try using the APPPATH constant when defining the paths for the includes.
I know it's an old question, but you can try using a view template with an iframe, and you can pass the URL to the src property of the iframe. That way you can display your site inside a view, but still can't get access to the vars passed to the view from your site.
In system/Core/Loader.php change the line 141 to look like this:
$this->_ci_view_paths = array(APPPATH . 'views/' => TRUE, FCPATH => TRUE);
and to get the view is simple:
$this->load->view('application/ PATH_TO_VIEW');